[BACK]Return to machine.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / top

Annotation of src/usr.bin/top/machine.c, Revision 1.28

1.28    ! tholo       1: /*     $OpenBSD: machine.c,v 1.27 2002/02/16 21:27:55 millert Exp $    */
        !             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.
        !            28:  */
1.1       downsj     29:
                     30: /*
                     31:  * top - a top users display for Unix
                     32:  *
                     33:  * SYNOPSIS:  For an OpenBSD system
                     34:  *
                     35:  * DESCRIPTION:
                     36:  * This is the machine-dependent module for OpenBSD
                     37:  * Tested on:
                     38:  *     i386
                     39:  *
                     40:  * TERMCAP: -ltermlib
                     41:  *
1.11      kstailey   42:  * CFLAGS: -DHAVE_GETOPT -DORDER
1.1       downsj     43:  *
                     44:  * AUTHOR:  Thorsten Lockert <tholo@sigmasoft.com>
                     45:  *          Adapted from BSD4.4 by Christos Zoulas <christos@ee.cornell.edu>
                     46:  *          Patch for process wait display by Jarl F. Greipsland <jarle@idt.unit.no>
1.11      kstailey   47:  *         Patch for -DORDER by Kenneth Stailey <kstailey@disclosure.com>
1.15      weingart   48:  *         Patch for new swapctl(2) by Tobias Weingartner <weingart@openbsd.org>
1.1       downsj     49:  */
                     50:
                     51: #include <sys/types.h>
                     52: #include <sys/signal.h>
                     53: #include <sys/param.h>
                     54:
                     55: #define DOSWAP
                     56:
                     57: #include <stdio.h>
                     58: #include <stdlib.h>
1.3       downsj     59: #include <string.h>
1.6       millert    60: #include <limits.h>
                     61: #include <err.h>
1.1       downsj     62: #include <math.h>
                     63: #include <unistd.h>
                     64: #include <sys/errno.h>
                     65: #include <sys/sysctl.h>
                     66: #include <sys/dir.h>
                     67: #include <sys/dkstat.h>
                     68: #include <sys/file.h>
                     69: #include <sys/time.h>
                     70: #include <sys/resource.h>
                     71:
                     72: #ifdef DOSWAP
1.15      weingart   73: #include <sys/swap.h>
1.1       downsj     74: #include <err.h>
                     75: #endif
                     76:
1.27      millert    77: static int swapmode(int *, int *);
1.1       downsj     78:
                     79: #include "top.h"
1.3       downsj     80: #include "display.h"
1.1       downsj     81: #include "machine.h"
                     82: #include "utils.h"
                     83:
                     84: /* get_process_info passes back a handle.  This is what it looks like: */
                     85:
1.20      deraadt    86: struct handle {
                     87:        struct kinfo_proc **next_proc;  /* points to next valid proc pointer */
                     88:        int     remaining;      /* number of pointers remaining */
1.1       downsj     89: };
                     90:
                     91: /* declarations for load_avg */
                     92: #include "loadavg.h"
                     93:
                     94: #define PP(pp, field) ((pp)->kp_proc . field)
                     95: #define EP(pp, field) ((pp)->kp_eproc . field)
                     96: #define VP(pp, field) ((pp)->kp_eproc.e_vm . field)
                     97:
                     98: /* what we consider to be process size: */
                     99: #define PROCSIZE(pp) (VP((pp), vm_tsize) + VP((pp), vm_dsize) + VP((pp), vm_ssize))
                    100:
                    101: /*
                    102:  *  These definitions control the format of the per-process area
                    103:  */
                    104: static char header[] =
1.20      deraadt   105: "  PID X        PRI NICE  SIZE   RES STATE WAIT     TIME    CPU COMMAND";
1.1       downsj    106: /* 0123456   -- field to fill in starts at header+6 */
                    107: #define UNAME_START 6
                    108:
                    109: #define Proc_format \
                    110:        "%5d %-8.8s %3d %4d %5s %5s %-5s %-6.6s %6s %5.2f%% %.14s"
                    111:
                    112:
                    113: /* process state names for the "STATE" column of the display */
                    114: /* the extra nulls in the string "run" are for adding a slash and
                    115:    the processor number when needed */
                    116:
1.20      deraadt   117: char *state_abbrev[] = {
                    118:        "", "start", "run\0\0\0", "sleep", "stop", "zomb",
1.1       downsj    119: };
                    120:
                    121:
1.20      deraadt   122: static int stathz;
1.1       downsj    123:
                    124: /* these are for calculating cpu state percentages */
1.13      niklas    125: static long cp_time[CPUSTATES];
                    126: static long cp_old[CPUSTATES];
                    127: static long cp_diff[CPUSTATES];
1.1       downsj    128:
                    129: /* these are for detailing the process states */
1.20      deraadt   130: int     process_states[7];
                    131: char   *procstatenames[] = {
                    132:        "", " starting, ", " running, ", " idle, ", " stopped, ", " zombie, ",
                    133:        NULL
1.1       downsj    134: };
                    135:
                    136: /* these are for detailing the cpu states */
1.20      deraadt   137: int     cpu_states[CPUSTATES];
                    138: char   *cpustatenames[] = {
                    139:        "user", "nice", "system", "interrupt", "idle", NULL
1.1       downsj    140: };
                    141:
                    142: /* these are for detailing the memory statistics */
1.20      deraadt   143: int     memory_stats[8];
                    144: char   *memorynames[] = {
                    145:        "Real: ", "K/", "K act/tot  ", "Free: ", "K  ",
1.1       downsj    146: #ifdef DOSWAP
1.20      deraadt   147:        "Swap: ", "K/", "K used/tot",
1.1       downsj    148: #endif
1.20      deraadt   149:        NULL
1.1       downsj    150: };
                    151:
1.11      kstailey  152: #ifdef ORDER
                    153: /* these are names given to allowed sorting orders -- first is default */
1.20      deraadt   154: char   *ordernames[] = {"cpu", "size", "res", "time", "pri", NULL};
1.11      kstailey  155: #endif
                    156:
1.1       downsj    157: /* these are for keeping track of the proc array */
                    158: static int nproc;
                    159: static int onproc = -1;
                    160: static int pref_len;
                    161: static struct kinfo_proc *pbase;
                    162: static struct kinfo_proc **pref;
                    163:
                    164: /* these are for getting the memory statistics */
                    165: static int pageshift;          /* log base 2 of the pagesize */
                    166:
                    167: /* define pagetok in terms of pageshift */
                    168: #define pagetok(size) ((size) << pageshift)
                    169:
1.26      art       170: int maxslp;
                    171:
1.1       downsj    172: int
1.18      deraadt   173: getstathz()
                    174: {
                    175:        struct clockinfo cinf;
1.20      deraadt   176:        size_t  size = sizeof(cinf);
                    177:        int     mib[2];
1.18      deraadt   178:
                    179:        mib[0] = CTL_KERN;
                    180:        mib[1] = KERN_CLOCKRATE;
                    181:        if (sysctl(mib, 2, &cinf, &size, NULL, 0) == -1)
                    182:                return (-1);
                    183:        return (cinf.stathz);
                    184: }
                    185:
                    186: int
1.1       downsj    187: machine_init(statics)
1.20      deraadt   188:        struct statics *statics;
1.1       downsj    189: {
1.25      deraadt   190:        int pagesize;
1.20      deraadt   191:
                    192:        stathz = getstathz();
                    193:        if (stathz == -1)
                    194:                return (-1);
                    195:
                    196:        pbase = NULL;
                    197:        pref = NULL;
                    198:        onproc = -1;
                    199:        nproc = 0;
                    200:
                    201:        /* get the page size with "getpagesize" and calculate pageshift from
                    202:         * it */
                    203:        pagesize = getpagesize();
                    204:        pageshift = 0;
                    205:        while (pagesize > 1) {
                    206:                pageshift++;
                    207:                pagesize >>= 1;
                    208:        }
                    209:
                    210:        /* we only need the amount of log(2)1024 for our conversion */
                    211:        pageshift -= LOG1024;
                    212:
                    213:        /* fill in the statics information */
                    214:        statics->procstate_names = procstatenames;
                    215:        statics->cpustate_names = cpustatenames;
                    216:        statics->memory_names = memorynames;
1.11      kstailey  217: #ifdef ORDER
1.20      deraadt   218:        statics->order_names = ordernames;
1.11      kstailey  219: #endif
1.20      deraadt   220:        return (0);
1.1       downsj    221: }
                    222:
1.20      deraadt   223: char *
                    224: format_header(uname_field)
                    225:        char   *uname_field;
1.1       downsj    226: {
1.20      deraadt   227:        char *ptr;
1.1       downsj    228:
1.20      deraadt   229:        ptr = header + UNAME_START;
                    230:        while (*uname_field != '\0') {
                    231:                *ptr++ = *uname_field++;
                    232:        }
                    233:        return (header);
1.1       downsj    234: }
                    235:
                    236: void
                    237: get_system_info(si)
1.20      deraadt   238:        struct system_info *si;
1.1       downsj    239: {
1.20      deraadt   240:        static int sysload_mib[] = {CTL_VM, VM_LOADAVG};
                    241:        static int vmtotal_mib[] = {CTL_VM, VM_METER};
1.21      deraadt   242:        static int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
1.1       downsj    243:        struct loadavg sysload;
1.20      deraadt   244:        struct vmtotal vmtotal;
                    245:        double *infoloadp;
                    246:        int total, i;
                    247:        size_t  size;
1.26      art       248:
1.21      deraadt   249:        size = sizeof(cp_time);
                    250:        if (sysctl(cp_time_mib, 2, &cp_time, &size, NULL, 0) < 0) {
                    251:                warn("sysctl kern.cp_time failed");
                    252:                total = 0;
                    253:        }
1.20      deraadt   254:
                    255:        size = sizeof(sysload);
                    256:        if (sysctl(sysload_mib, 2, &sysload, &size, NULL, 0) < 0) {
                    257:                warn("sysctl failed");
1.22      deraadt   258:                total = 0;
1.1       downsj    259:        }
                    260:        infoloadp = si->load_avg;
                    261:        for (i = 0; i < 3; i++)
1.20      deraadt   262:                *infoloadp++ = ((double) sysload.ldavg[i]) / sysload.fscale;
1.1       downsj    263:
1.20      deraadt   264:        /* convert cp_time counts to percentages */
                    265:        total = percentages(CPUSTATES, cpu_states, cp_time, cp_old, cp_diff);
1.1       downsj    266:
                    267:        /* get total -- systemwide main memory usage structure */
1.20      deraadt   268:        size = sizeof(vmtotal);
                    269:        if (sysctl(vmtotal_mib, 2, &vmtotal, &size, NULL, 0) < 0) {
                    270:                warn("sysctl failed");
                    271:                bzero(&vmtotal, sizeof(vmtotal));
1.1       downsj    272:        }
                    273:        /* convert memory stats to Kbytes */
                    274:        memory_stats[0] = -1;
1.20      deraadt   275:        memory_stats[1] = pagetok(vmtotal.t_arm);
                    276:        memory_stats[2] = pagetok(vmtotal.t_rm);
1.1       downsj    277:        memory_stats[3] = -1;
1.20      deraadt   278:        memory_stats[4] = pagetok(vmtotal.t_free);
1.1       downsj    279:        memory_stats[5] = -1;
                    280: #ifdef DOSWAP
                    281:        if (!swapmode(&memory_stats[6], &memory_stats[7])) {
1.20      deraadt   282:                memory_stats[6] = 0;
                    283:                memory_stats[7] = 0;
1.1       downsj    284:        }
                    285: #endif
                    286:
1.20      deraadt   287:        /* set arrays and strings */
                    288:        si->cpustates = cpu_states;
                    289:        si->memory = memory_stats;
                    290:        si->last_pid = -1;
1.1       downsj    291: }
                    292:
                    293: static struct handle handle;
                    294:
1.22      deraadt   295: struct kinfo_proc *
                    296: getprocs(op, arg, cnt)
                    297:        int op, arg;
                    298:        int *cnt;
                    299: {
1.24      angelos   300:        size_t size = sizeof(int);
1.22      deraadt   301:        int mib[4] = {CTL_KERN, KERN_PROC, op, arg};
1.24      angelos   302:        int smib[2] = {CTL_KERN, KERN_NPROCS};
1.26      art       303:        static int maxslp_mib[] = {CTL_VM, VM_MAXSLP};
1.23      deraadt   304:        static struct kinfo_proc *procbase;
1.24      angelos   305:        int st;
1.22      deraadt   306:
1.26      art       307:        size = sizeof(maxslp);
                    308:        if (sysctl(maxslp_mib, 2, &maxslp, &size, NULL, 0) < 0) {
                    309:                warn("sysctl vm.maxslp failed");
                    310:                return (0);
                    311:        }
                    312:
1.24      angelos   313:        st = sysctl(smib, 2, cnt, &size, NULL, 0);
1.22      deraadt   314:        if (st == -1) {
                    315:                /* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */
                    316:                return (0);
                    317:        }
1.23      deraadt   318:        if (procbase)
                    319:                free(procbase);
1.24      angelos   320:        size = (6 * (*cnt) * sizeof(struct kinfo_proc)) / 5;
1.22      deraadt   321:        procbase = (struct kinfo_proc *)malloc(size);
                    322:        if (procbase == NULL)
                    323:                return (0);
                    324:        st = sysctl(mib, 4, procbase, &size, NULL, 0);
                    325:        if (st == -1) {
                    326:                /* _kvm_syserr(kd, kd->program, "kvm_getprocs"); */
                    327:                return (0);
                    328:        }
                    329:        if (size % sizeof(struct kinfo_proc) != 0) {
                    330:                /* _kvm_err(kd, kd->program,
                    331:                    "proc size mismatch (%d total, %d chunks)",
                    332:                    size, sizeof(struct kinfo_proc)); */
                    333:                return (0);
                    334:        }
                    335:        return (procbase);
                    336: }
                    337:
1.20      deraadt   338: caddr_t
                    339: get_process_info(si, sel, compare)
                    340:        struct system_info *si;
                    341:        struct process_select *sel;
1.27      millert   342:        int (*compare)(const void *, const void *);
1.20      deraadt   343:
                    344: {
                    345:        int show_idle, show_system, show_uid, show_command;
                    346:        int total_procs, active_procs, i;
                    347:        struct kinfo_proc **prefp, *pp;
                    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)
                    354:                pref = (struct kinfo_proc **) realloc(pref, sizeof(struct kinfo_proc *)
                    355:                    * (onproc = nproc));
                    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;
                    366:        show_uid = sel->uid != -1;
                    367:        show_command = sel->command != NULL;
                    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;
                    374:        for (pp = pbase, i = 0; i < nproc; pp++, i++) {
                    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:                 */
                    381:                if (PP(pp, p_stat) != 0 &&
                    382:                    (show_system || ((PP(pp, p_flag) & P_SYSTEM) == 0))) {
                    383:                        total_procs++;
                    384:                        process_states[(unsigned char) PP(pp, p_stat)]++;
                    385:                        if ((PP(pp, p_stat) != SZOMB) &&
                    386:                            (show_idle || (PP(pp, p_pctcpu) != 0) ||
                    387:                                (PP(pp, p_stat) == SRUN)) &&
                    388:                            (!show_uid || EP(pp, e_pcred.p_ruid) == (uid_t) sel->uid)) {
                    389:                                *prefp++ = pp;
                    390:                                active_procs++;
                    391:                        }
                    392:                }
                    393:        }
                    394:
                    395:        /* if requested, sort the "interesting" processes */
                    396:        if (compare != NULL) {
                    397:                qsort((char *) pref, active_procs, sizeof(struct kinfo_proc *), compare);
                    398:        }
                    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:
                    409: char    fmt[MAX_COLS];         /* static area where result is built */
                    410:
                    411: char *
                    412: format_next_process(handle, get_userid)
                    413:        caddr_t handle;
                    414:        char *(*get_userid)();
                    415:
                    416: {
                    417:        char waddr[sizeof(void *) * 2 + 3];     /* Hexify void pointer */
                    418:        struct kinfo_proc *pp;
                    419:        struct handle *hp;
                    420:        char *p_wait;
                    421:        int cputime;
                    422:        double pct;
                    423:
                    424:        /* find and remember the next proc structure */
                    425:        hp = (struct handle *) handle;
                    426:        pp = *(hp->next_proc++);
                    427:        hp->remaining--;
                    428:
                    429:        /* get the process's user struct and set cputime */
                    430:        if ((PP(pp, p_flag) & P_INMEM) == 0) {
                    431:                /*
                    432:                 * Print swapped processes as <pname>
                    433:                 */
                    434:                char   *comm = PP(pp, p_comm);
1.1       downsj    435: #define COMSIZ sizeof(PP(pp, p_comm))
1.20      deraadt   436:                char    buf[COMSIZ];
                    437:                (void) strncpy(buf, comm, COMSIZ);
                    438:                comm[0] = '<';
                    439:                (void) strncpy(&comm[1], buf, COMSIZ - 2);
                    440:                comm[COMSIZ - 2] = '\0';
                    441:                (void) strncat(comm, ">", COMSIZ - 1);
                    442:                comm[COMSIZ - 1] = '\0';
                    443:        }
                    444:        cputime = (PP(pp, p_uticks) + PP(pp, p_sticks) + PP(pp, p_iticks)) / stathz;
                    445:
                    446:        /* calculate the base for cpu percentages */
                    447:        pct = pctdouble(PP(pp, p_pctcpu));
                    448:
                    449:        if (PP(pp, p_wchan))
                    450:                if (PP(pp, p_wmesg))
                    451:                        p_wait = EP(pp, e_wmesg);
                    452:                else {
                    453:                        snprintf(waddr, sizeof(waddr), "%lx",
                    454:                            (unsigned long) (PP(pp, p_wchan)) & ~KERNBASE);
                    455:                        p_wait = waddr;
                    456:                }
                    457:        else
                    458:                p_wait = "-";
                    459:
                    460:        /* format this entry */
                    461:        snprintf(fmt, MAX_COLS,
1.1       downsj    462:            Proc_format,
                    463:            PP(pp, p_pid),
1.20      deraadt   464:            (*get_userid) (EP(pp, e_pcred.p_ruid)),
1.1       downsj    465:            PP(pp, p_priority) - PZERO,
                    466:            PP(pp, p_nice) - NZERO,
                    467:            format_k(pagetok(PROCSIZE(pp))),
                    468:            format_k(pagetok(VP(pp, vm_rssize))),
1.26      art       469:            (PP(pp, p_stat) == SSLEEP && PP(pp, p_slptime) > maxslp)
1.20      deraadt   470:            ? "idle" : state_abbrev[(unsigned char) PP(pp, p_stat)],
1.1       downsj    471:            p_wait,
                    472:            format_time(cputime),
                    473:            100.0 * pct,
                    474:            printable(PP(pp, p_comm)));
                    475:
1.20      deraadt   476:        /* return the result */
                    477:        return (fmt);
1.1       downsj    478: }
                    479:
                    480: /* comparison routine for qsort */
1.11      kstailey  481: static unsigned char sorted_state[] =
                    482: {
1.20      deraadt   483:        0,                      /* not used              */
                    484:        4,                      /* start                 */
                    485:        5,                      /* run                   */
                    486:        2,                      /* sleep                 */
                    487:        3,                      /* stop                  */
                    488:        1                       /* zombie                */
1.11      kstailey  489: };
                    490: #ifdef ORDER
                    491:
                    492: /*
                    493:  *  proc_compares - comparison functions for "qsort"
                    494:  */
                    495:
                    496: /*
                    497:  * First, the possible comparison keys.  These are defined in such a way
                    498:  * that they can be merely listed in the source code to define the actual
                    499:  * desired ordering.
                    500:  */
                    501:
                    502:
                    503: #define ORDERKEY_PCTCPU \
1.12      niklas    504:        if (lresult = (pctcpu)PP(p2, p_pctcpu) - (pctcpu)PP(p1, p_pctcpu), \
1.22      deraadt   505:            (result = lresult > 0 ? 1 : lresult < 0 ? -1 : 0) == 0)
1.11      kstailey  506: #define ORDERKEY_CPUTIME \
                    507:        if ((result = PP(p2, p_rtime.tv_sec) - PP(p1, p_rtime.tv_sec)) == 0) \
                    508:                if ((result = PP(p2, p_rtime.tv_usec) - \
                    509:                     PP(p1, p_rtime.tv_usec)) == 0)
                    510: #define ORDERKEY_STATE \
                    511:        if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] - \
1.22      deraadt   512:            sorted_state[(unsigned char) PP(p1, p_stat)])  == 0)
1.11      kstailey  513: #define ORDERKEY_PRIO \
                    514:        if ((result = PP(p2, p_priority) - PP(p1, p_priority)) == 0)
                    515: #define ORDERKEY_RSSIZE \
                    516:        if ((result = VP(p2, vm_rssize) - VP(p1, vm_rssize)) == 0)
                    517: #define ORDERKEY_MEM \
                    518:        if ((result = PROCSIZE(p2) - PROCSIZE(p1)) == 0)
                    519:
1.20      deraadt   520:
1.11      kstailey  521: /* compare_cpu - the comparison function for sorting by cpu percentage */
                    522: int
                    523: compare_cpu(v1, v2)
1.20      deraadt   524:        const void *v1, *v2;
1.11      kstailey  525: {
1.20      deraadt   526:        struct proc **pp1 = (struct proc **) v1;
                    527:        struct proc **pp2 = (struct proc **) v2;
                    528:        struct kinfo_proc *p1;
                    529:        struct kinfo_proc *p2;
                    530:        int result;
                    531:        pctcpu lresult;
                    532:
                    533:        /* remove one level of indirection */
                    534:        p1 = *(struct kinfo_proc **) pp1;
                    535:        p2 = *(struct kinfo_proc **) pp2;
                    536:
                    537:        ORDERKEY_PCTCPU
                    538:            ORDERKEY_CPUTIME
                    539:            ORDERKEY_STATE
                    540:            ORDERKEY_PRIO
                    541:            ORDERKEY_RSSIZE
                    542:            ORDERKEY_MEM
                    543:            ;
                    544:        return (result);
1.11      kstailey  545: }
                    546:
                    547: /* compare_size - the comparison function for sorting by total memory usage */
                    548: int
                    549: compare_size(v1, v2)
1.20      deraadt   550:        const void *v1, *v2;
1.11      kstailey  551: {
1.20      deraadt   552:        struct proc **pp1 = (struct proc **) v1;
                    553:        struct proc **pp2 = (struct proc **) v2;
                    554:        struct kinfo_proc *p1;
                    555:        struct kinfo_proc *p2;
                    556:        int result;
                    557:        pctcpu lresult;
                    558:
                    559:        /* remove one level of indirection */
                    560:        p1 = *(struct kinfo_proc **) pp1;
                    561:        p2 = *(struct kinfo_proc **) pp2;
                    562:
                    563:        ORDERKEY_MEM
                    564:            ORDERKEY_RSSIZE
                    565:            ORDERKEY_PCTCPU
                    566:            ORDERKEY_CPUTIME
                    567:            ORDERKEY_STATE
                    568:            ORDERKEY_PRIO
                    569:            ;
                    570:        return (result);
1.11      kstailey  571: }
                    572:
                    573: /* compare_res - the comparison function for sorting by resident set size */
                    574: int
                    575: compare_res(v1, v2)
1.20      deraadt   576:        const void *v1, *v2;
1.11      kstailey  577: {
1.20      deraadt   578:        struct proc **pp1 = (struct proc **) v1;
                    579:        struct proc **pp2 = (struct proc **) v2;
                    580:        struct kinfo_proc *p1;
                    581:        struct kinfo_proc *p2;
                    582:        int result;
                    583:        pctcpu lresult;
                    584:
                    585:        /* remove one level of indirection */
                    586:        p1 = *(struct kinfo_proc **) pp1;
                    587:        p2 = *(struct kinfo_proc **) pp2;
                    588:
                    589:        ORDERKEY_RSSIZE
                    590:            ORDERKEY_MEM
                    591:            ORDERKEY_PCTCPU
                    592:            ORDERKEY_CPUTIME
                    593:            ORDERKEY_STATE
                    594:            ORDERKEY_PRIO
                    595:            ;
                    596:        return (result);
1.11      kstailey  597: }
                    598:
                    599: /* compare_time - the comparison function for sorting by CPU time */
                    600: int
                    601: compare_time(v1, v2)
1.20      deraadt   602:        const void *v1, *v2;
1.11      kstailey  603: {
1.20      deraadt   604:        struct proc **pp1 = (struct proc **) v1;
                    605:        struct proc **pp2 = (struct proc **) v2;
                    606:        struct kinfo_proc *p1;
                    607:        struct kinfo_proc *p2;
                    608:        int result;
                    609:        pctcpu lresult;
                    610:
                    611:        /* remove one level of indirection */
                    612:        p1 = *(struct kinfo_proc **) pp1;
                    613:        p2 = *(struct kinfo_proc **) pp2;
                    614:
                    615:        ORDERKEY_CPUTIME
                    616:            ORDERKEY_PCTCPU
                    617:            ORDERKEY_STATE
                    618:            ORDERKEY_PRIO
                    619:            ORDERKEY_MEM
                    620:            ORDERKEY_RSSIZE
                    621:            ;
                    622:        return (result);
1.11      kstailey  623: }
                    624:
                    625: /* compare_prio - the comparison function for sorting by CPU time */
                    626: int
                    627: compare_prio(v1, v2)
1.20      deraadt   628:        const void *v1, *v2;
1.11      kstailey  629: {
1.20      deraadt   630:        struct proc **pp1 = (struct proc **) v1;
                    631:        struct proc **pp2 = (struct proc **) v2;
                    632:        struct kinfo_proc *p1;
                    633:        struct kinfo_proc *p2;
                    634:        int result;
                    635:        pctcpu lresult;
                    636:
                    637:        /* remove one level of indirection */
                    638:        p1 = *(struct kinfo_proc **) pp1;
                    639:        p2 = *(struct kinfo_proc **) pp2;
                    640:
                    641:        ORDERKEY_PRIO
                    642:            ORDERKEY_PCTCPU
                    643:            ORDERKEY_CPUTIME
                    644:            ORDERKEY_STATE
                    645:            ORDERKEY_RSSIZE
                    646:            ORDERKEY_MEM
                    647:            ;
                    648:        return (result);
                    649: }
                    650:
                    651: int     (*proc_compares[]) () = {
                    652:        compare_cpu,
                    653:        compare_size,
                    654:        compare_res,
                    655:        compare_time,
                    656:        compare_prio,
                    657:        NULL
1.11      kstailey  658: };
1.20      deraadt   659: #else
1.1       downsj    660: /*
                    661:  *  proc_compare - comparison function for "qsort"
                    662:  *     Compares the resource consumption of two processes using five
                    663:  *     distinct keys.  The keys (in descending order of importance) are:
                    664:  *     percent cpu, cpu ticks, state, resident set size, total virtual
                    665:  *     memory usage.  The process states are ordered as follows (from least
                    666:  *     to most important):  zombie, sleep, stop, start, run.  The array
                    667:  *     declaration below maps a process state index into a number that
                    668:  *     reflects this ordering.
                    669:  */
                    670: int
1.3       downsj    671: proc_compare(v1, v2)
1.20      deraadt   672:        const void *v1, *v2;
1.1       downsj    673: {
1.20      deraadt   674:        struct proc **pp1 = (struct proc **) v1;
                    675:        struct proc **pp2 = (struct proc **) v2;
                    676:        struct kinfo_proc *p1;
                    677:        struct kinfo_proc *p2;
                    678:        int result;
                    679:        pctcpu lresult;
                    680:
                    681:        /* remove one level of indirection */
                    682:        p1 = *(struct kinfo_proc **) pp1;
                    683:        p2 = *(struct kinfo_proc **) pp2;
                    684:
                    685:        /* compare percent cpu (pctcpu) */
                    686:        if ((lresult = PP(p2, p_pctcpu) - PP(p1, p_pctcpu)) == 0) {
                    687:                /* use CPU usage to break the tie */
                    688:                if ((result = PP(p2, p_rtime).tv_sec - PP(p1, p_rtime).tv_sec) == 0) {
                    689:                        /* use process state to break the tie */
                    690:                        if ((result = sorted_state[(unsigned char) PP(p2, p_stat)] -
                    691:                                sorted_state[(unsigned char) PP(p1, p_stat)]) == 0) {
                    692:                                /* use priority to break the tie */
1.22      deraadt   693:                                if ((result = PP(p2, p_priority) -
                    694:                                    PP(p1, p_priority)) == 0) {
1.20      deraadt   695:                                        /* use resident set size (rssize) to
                    696:                                         * break the tie */
1.22      deraadt   697:                                        if ((result = VP(p2, vm_rssize) -
                    698:                                            VP(p1, vm_rssize)) == 0) {
1.20      deraadt   699:                                                /* use total memory to break
                    700:                                                 * the tie */
                    701:                                                result = PROCSIZE(p2) - PROCSIZE(p1);
                    702:                                        }
                    703:                                }
                    704:                        }
1.1       downsj    705:                }
1.20      deraadt   706:        } else {
                    707:                result = lresult < 0 ? -1 : 1;
1.1       downsj    708:        }
1.20      deraadt   709:        return (result);
1.1       downsj    710: }
1.11      kstailey  711: #endif
1.1       downsj    712:
                    713: /*
                    714:  * proc_owner(pid) - returns the uid that owns process "pid", or -1 if
                    715:  *             the process does not exist.
                    716:  *             It is EXTREMLY IMPORTANT that this function work correctly.
                    717:  *             If top runs setuid root (as in SVR4), then this function
                    718:  *             is the only thing that stands in the way of a serious
                    719:  *             security problem.  It validates requests for the "kill"
                    720:  *             and "renice" commands.
                    721:  */
1.20      deraadt   722: int
                    723: proc_owner(pid)
                    724:        pid_t   pid;
                    725: {
                    726:        struct kinfo_proc **prefp, *pp;
                    727:        int cnt;
                    728:
                    729:        prefp = pref;
                    730:        cnt = pref_len;
                    731:        while (--cnt >= 0) {
                    732:                pp = *prefp++;
                    733:                if (PP(pp, p_pid) == pid) {
                    734:                        return ((int) EP(pp, e_pcred.p_ruid));
                    735:                }
1.1       downsj    736:        }
1.20      deraadt   737:        return (-1);
1.1       downsj    738: }
                    739: #ifdef DOSWAP
                    740: /*
1.17      todd      741:  * swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
1.15      weingart  742:  * to be based on the new swapctl(2) system call.
1.1       downsj    743:  */
                    744: static int
                    745: swapmode(used, total)
1.20      deraadt   746:        int    *used;
                    747:        int    *total;
1.1       downsj    748: {
1.20      deraadt   749:        int     nswap, rnswap, i;
1.15      weingart  750:        struct swapent *swdev;
1.1       downsj    751:
1.15      weingart  752:        nswap = swapctl(SWAP_NSWAP, 0, 0);
1.20      deraadt   753:        if (nswap == 0)
1.15      weingart  754:                return 0;
                    755:
                    756:        swdev = malloc(nswap * sizeof(*swdev));
1.20      deraadt   757:        if (swdev == NULL)
1.15      weingart  758:                return 0;
                    759:
                    760:        rnswap = swapctl(SWAP_STATS, swdev, nswap);
1.20      deraadt   761:        if (rnswap == -1)
1.15      weingart  762:                return 0;
                    763:
                    764:        /* if rnswap != nswap, then what? */
                    765:
                    766:        /* Total things up */
                    767:        *total = *used = 0;
                    768:        for (i = 0; i < nswap; i++) {
                    769:                if (swdev[i].se_flags & SWF_ENABLE) {
1.20      deraadt   770:                        *used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
                    771:                        *total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
1.1       downsj    772:                }
                    773:        }
                    774:
1.20      deraadt   775:        free(swdev);
1.1       downsj    776:        return 1;
                    777: }
                    778: #endif