Annotation of 41BSD/cmd/ps.c, revision 1.1.1.1

1.1       root        1: static char *sccsid = "@(#)ps.c        4.7 (Berkeley) 10/20/80";
                      2: /*
                      3:  * ps; VAX 4BSD version
                      4:  */
                      5: 
                      6: #include <stdio.h>
                      7: #include <ctype.h>
                      8: #include <nlist.h>
                      9: #include <pwd.h>
                     10: #include <sys/param.h>
                     11: #include <sys/tty.h>
                     12: #include <sys/dir.h>
                     13: #include <sys/user.h>
                     14: #include <sys/proc.h>
                     15: #include <sys/pte.h>
                     16: #include <sys/vm.h>
                     17: #include <sys/text.h>
                     18: #include <sys/stat.h>
                     19: #include <math.h>
                     20: 
                     21: struct nlist nl[] = {
                     22:        { "_proc" },
                     23: #define        X_PROC          0
                     24:        { "_Usrptmap" },
                     25: #define        X_USRPTMA       1
                     26:        { "_usrpt" },
                     27: #define        X_USRPT         2
                     28:        { "_text" },
                     29: #define        X_TEXT          3
                     30:        { "_nswap" },
                     31: #define        X_NSWAP         4
                     32:        { "_maxslp" },
                     33: #define        X_MAXSLP        5
                     34:        { "_ccpu" },
                     35: #define        X_CCPU          6
                     36:        { "_ecmx" },
                     37: #define        X_ECMX          7
                     38:        { 0 },
                     39: };
                     40: 
                     41: struct savcom {
                     42:        union {
                     43:                struct  lsav *lp;
                     44:                float   u_pctcpu;
                     45:                struct  vsav *vp;
                     46:                int     s_ssiz;
                     47:        } sun;
                     48:        struct  asav *ap;
                     49: } savcom[NPROC];
                     50: 
                     51: struct asav {
                     52:        char    *a_cmdp;
                     53:        int     a_flag;
                     54:        short   a_stat, a_uid, a_pid, a_nice, a_pri, a_slptime, a_time;
                     55:        size_t  a_size, a_rss, a_tsiz, a_txtrss;
                     56:        short   a_xccount;
                     57:        char    a_tty[DIRSIZ+1];
                     58:        dev_t   a_ttyd;
                     59:        time_t  a_cpu;
                     60: };
                     61: 
                     62: char   *lhdr;
                     63: struct lsav {
                     64:        short   l_ppid;
                     65:        char    l_cpu;
                     66:        int     l_addr;
                     67:        caddr_t l_wchan;
                     68: };
                     69: 
                     70: char   *uhdr;
                     71: char   *shdr;
                     72: 
                     73: char   *vhdr;
                     74: struct vsav {
                     75:        u_int   v_majflt;
                     76:        size_t  v_swrss, v_txtswrss;
                     77:        float   v_pctcpu;
                     78: };
                     79: 
                     80: struct proc proc[8];           /* 8 = a few, for less syscalls */
                     81: struct proc *mproc;
                     82: struct text *text;
                     83: 
                     84: int    paduser1;               /* avoid hardware mem clobbering botch */
                     85: union {
                     86:        struct  user user;
                     87:        char    upages[UPAGES][NBPG];
                     88: } user;
                     89: #define u      user.user
                     90: int    paduser2;               /* avoid hardware mem clobbering botch */
                     91: 
                     92: #define clear(x)       ((int)x & 0x7fffffff)
                     93: 
                     94: int    chkpid;
                     95: int    aflg, cflg, eflg, gflg, kflg, lflg, sflg, uflg, vflg, xflg;
                     96: char   *tptr;
                     97: char   *gettty(), *getcmd(), *getname(), *savestr(), *alloc(), *state();
                     98: double pcpu(), pmem();
                     99: int    pscomp();
                    100: int    nswap, maxslp;
                    101: double ccpu;
                    102: int    ecmx;
                    103: struct pte *Usrptma, *usrpt;
                    104: 
                    105: struct ttys {
                    106:        char    name[DIRSIZ+1];
                    107:        dev_t   ttyd;
                    108:        struct  ttys *next;
                    109:        struct  ttys *cand;
                    110: } *allttys, *cand[16];
                    111: 
                    112: struct savcom savcom[NPROC];
                    113: int    npr;
                    114: 
                    115: int    cmdstart;
                    116: int    twidth;
                    117: char   *kmemf, *memf, *swapf, *nlistf;
                    118: int    kmem, mem, swap;
                    119: int    rawcpu, sumcpu;
                    120: 
                    121: int    pcbpf;
                    122: int    argaddr;
                    123: extern char _sobuf[];
                    124: 
                    125: main(argc, argv)
                    126:        char **argv;
                    127: {
                    128:        register int i, j;
                    129:        register char *ap;
                    130:        int uid;
                    131:        off_t procp;
                    132: 
                    133:        if (chdir("/dev") < 0) {
                    134:                perror("/dev");
                    135:                exit(1);
                    136:        }
                    137:        twidth = 80;
                    138:        setbuf(stdout, _sobuf);
                    139:        argc--, argv++;
                    140:        if (argc > 0) {
                    141:                ap = argv[0];
                    142:                while (*ap) switch (*ap++) {
                    143: 
                    144:                case 'C':
                    145:                        rawcpu++;
                    146:                        break;
                    147:                case 'S':
                    148:                        sumcpu++;
                    149:                        break;
                    150:                case 'a':
                    151:                        aflg++;
                    152:                        break;
                    153:                case 'c':
                    154:                        cflg = !cflg;
                    155:                        break;
                    156:                case 'e':
                    157:                        eflg++;
                    158:                        break;
                    159:                case 'g':
                    160:                        gflg++;
                    161:                        break;
                    162:                case 'k':
                    163:                        kflg++;
                    164:                        break;
                    165:                case 'l':
                    166:                        lflg++;
                    167:                        break;
                    168:                case 's':
                    169:                        sflg++;
                    170:                        break;
                    171:                case 't':
                    172:                        if (*ap)
                    173:                                tptr = ap;
                    174:                        aflg++;
                    175:                        gflg++;
                    176:                        if (*tptr == '?')
                    177:                                xflg++;
                    178:                        while (*ap)
                    179:                                ap++;
                    180:                        break;
                    181:                case 'u':
                    182:                        uflg++;
                    183:                        break;
                    184:                case 'v':
                    185:                        cflg = 1;
                    186:                        vflg++;
                    187:                        break;
                    188:                case 'w':
                    189:                        if (twidth == 80)
                    190:                                twidth = 132;
                    191:                        else
                    192:                                twidth = BUFSIZ;
                    193:                        break;
                    194:                case 'x':
                    195:                        xflg++;
                    196:                        break;
                    197:                default:
                    198:                        if (!isdigit(ap[-1]))
                    199:                                break;
                    200:                        chkpid = atoi(--ap);
                    201:                        *ap = 0;
                    202:                        aflg++;
                    203:                        xflg++;
                    204:                        break;
                    205:                }
                    206:        }
                    207:        openfiles(argc, argv);
                    208:        getkvars(argc, argv);
                    209:        getdev();
                    210:        uid = getuid();
                    211:        printhdr();
                    212:        procp = nl[X_PROC].n_value;
                    213:        for (i=0; i<NPROC; i += 8) {
                    214:                lseek(kmem, (char *)procp, 0);
                    215:                j = NPROC - i;
                    216:                if (j > 8)
                    217:                        j = 8;
                    218:                j *= sizeof (struct proc);
                    219:                if (read(kmem, (char *)proc, j) != j)
                    220:                        cantread("proc table", kmemf);
                    221:                procp += j;
                    222:                for (j = j / sizeof (struct proc) - 1; j >= 0; j--) {
                    223:                        mproc = &proc[j];
                    224:                        if (mproc->p_stat == 0 ||
                    225:                            mproc->p_pgrp == 0 && xflg == 0)
                    226:                                continue;
                    227:                        if (tptr == 0 && gflg == 0 && xflg == 0 &&
                    228:                            mproc->p_ppid == 1 && (mproc->p_flag&SDETACH) == 0)
                    229:                                continue;
                    230:                        if (uid != mproc->p_uid && aflg==0 ||
                    231:                            chkpid != 0 && chkpid != mproc->p_pid)
                    232:                                continue;
                    233:                        if (vflg && gflg == 0 && xflg == 0) {
                    234:                                if (mproc->p_stat == SZOMB ||
                    235:                                    mproc->p_flag&SWEXIT)
                    236:                                        continue;
                    237:                                if (mproc->p_slptime > MAXSLP &&
                    238:                                    (mproc->p_stat == SSLEEP ||
                    239:                                     mproc->p_stat == SSTOP))
                    240:                                continue;
                    241:                        }
                    242:                        save();
                    243:                }
                    244:        }
                    245:        qsort(savcom, npr, sizeof(savcom[0]), pscomp);
                    246:        for (i=0; i<npr; i++) {
                    247:                register struct savcom *sp = &savcom[i];
                    248:                if (lflg)
                    249:                        lpr(sp);
                    250:                else if (vflg)
                    251:                        vpr(sp);
                    252:                else if (uflg)
                    253:                        upr(sp);
                    254:                else
                    255:                        spr(sp);
                    256:                if (sp->ap->a_flag & SWEXIT)
                    257:                        printf(" <exiting>");
                    258:                else if (sp->ap->a_stat == SZOMB)
                    259:                        printf(" <defunct>");
                    260:                else if (sp->ap->a_pid == 0)
                    261:                        printf(" swapper");
                    262:                else if (sp->ap->a_pid == 2)
                    263:                        printf(" pagedaemon");
                    264:                else
                    265:                        printf(" %.*s", twidth - cmdstart - 2, sp->ap->a_cmdp);
                    266:                printf("\n");
                    267:        }
                    268:        exit(npr == 0);
                    269: }
                    270: 
                    271: openfiles(argc, argv)
                    272:        char **argv;
                    273: {
                    274: 
                    275:        kmemf = "kmem";
                    276:        if (kflg)
                    277:                kmemf = argc > 1 ? argv[1] : "/vmcore";
                    278:        kmem = open(kmemf, 0);
                    279:        if (kmem < 0) {
                    280:                perror(kmemf);
                    281:                exit(1);
                    282:        }
                    283:        if (kflg)  {
                    284:                mem = kmem;
                    285:                memf = kmemf;
                    286:        } else {
                    287:                memf = "mem";
                    288:                mem = open(memf, 0);
                    289:                if (mem < 0) {
                    290:                        perror(memf);
                    291:                        exit(1);
                    292:                }
                    293:        }
                    294:        swapf = argc>2 ? argv[2]: "drum";
                    295:        swap = open(swapf, 0);
                    296:        if (swap < 0) {
                    297:                perror(swapf);
                    298:                exit(1);
                    299:        }
                    300: }
                    301: 
                    302: getkvars(argc, argv)
                    303:        char **argv;
                    304: {
                    305:        register struct nlist *nlp;
                    306: 
                    307:        nlistf = argc > 3 ? argv[3] : "/vmunix";
                    308:        nlist(nlistf, nl);
                    309:        if (nl[0].n_type == 0) {
                    310:                fprintf(stderr, "%s: No namelist\n", nlistf);
                    311:                exit(1);
                    312:        }
                    313:        if (kflg)
                    314:                for (nlp = nl; nlp < &nl[sizeof (nl)/sizeof (nl[0])]; nlp++)
                    315:                        nlp->n_value = clear(nlp->n_value);
                    316:        Usrptma = (struct pte *)nl[X_USRPTMA].n_value;
                    317:        usrpt = (struct pte *)nl[X_USRPT].n_value;
                    318:        lseek(kmem, (long)nl[X_NSWAP].n_value, 0);
                    319:        if (read(kmem, &nswap, sizeof (nswap)) != sizeof (nswap)) {
                    320:                cantread("nswap", kmemf);
                    321:                exit(1);
                    322:        }
                    323:        lseek(kmem, (long)nl[X_MAXSLP].n_value, 0);
                    324:        if (read(kmem, &maxslp, sizeof (maxslp)) != sizeof (maxslp)) {
                    325:                cantread("maxslp", kmemf);
                    326:                exit(1);
                    327:        }
                    328:        lseek(kmem, (long)nl[X_CCPU].n_value, 0);
                    329:        if (read(kmem, &ccpu, sizeof (ccpu)) != sizeof (ccpu)) {
                    330:                cantread("ccpu", kmemf);
                    331:                exit(1);
                    332:        }
                    333:        lseek(kmem, (long)nl[X_ECMX].n_value, 0);
                    334:        if (read(kmem, &ecmx, sizeof (ecmx)) != sizeof (ecmx)) {
                    335:                cantread("ecmx", kmemf);
                    336:                exit(1);
                    337:        }
                    338:        if (uflg || vflg) {
                    339:                text = (struct text *)alloc(NTEXT * sizeof (struct text));
                    340:                if (text == 0) {
                    341:                        fprintf(stderr, "no room for text table\n");
                    342:                        exit(1);
                    343:                }
                    344:                lseek(kmem, (long)nl[X_TEXT].n_value, 0);
                    345:                if (read(kmem, (char *)text, NTEXT * sizeof (struct text))
                    346:                    != NTEXT * sizeof (struct text)) {
                    347:                        cantread("text table", kmemf);
                    348:                        exit(1);
                    349:                }
                    350:        }
                    351: }
                    352: 
                    353: printhdr()
                    354: {
                    355:        char *hdr;
                    356: 
                    357:        if (sflg+lflg+vflg+uflg > 1) {
                    358:                fprintf(stderr, "ps: specify only one of s,l,v and u\n");
                    359:                exit(1);
                    360:        }
                    361:        hdr = lflg ? lhdr : (vflg ? vhdr : (uflg ? uhdr : shdr));
                    362:        if (lflg+vflg+uflg+sflg == 0)
                    363:                hdr += strlen("SSIZ ");
                    364:        cmdstart = strlen(hdr);
                    365:        printf("%s COMMAND\n", hdr);
                    366:        fflush(stdout);
                    367: }
                    368: 
                    369: cantread(what, fromwhat)
                    370:        char *what, *fromwhat;
                    371: {
                    372: 
                    373:        fprintf(stderr, "ps: error reading %s from %s", what, fromwhat);
                    374: }
                    375: 
                    376: struct direct dbuf;
                    377: int    dialbase;
                    378: 
                    379: getdev()
                    380: {
                    381:        register FILE *df;
                    382:        register struct ttys *dp;
                    383: 
                    384:        dialbase = -1;
                    385:        if ((df = fopen(".", "r")) == NULL) {
                    386:                fprintf(stderr, "Can't open . in /dev\n");
                    387:                exit(1);
                    388:        }
                    389:        while (fread((char *)&dbuf, sizeof(dbuf), 1, df) == 1) {
                    390:                if (dbuf.d_ino == 0)
                    391:                        continue;
                    392:                maybetty(dp);
                    393:        }
                    394:        fclose(df);
                    395: }
                    396: 
                    397: /*
                    398:  * Attempt to avoid stats by guessing minor device
                    399:  * numbers from tty names.  Console is known,
                    400:  * know that r(hp|up|mt) are unlikely as are different mem's,
                    401:  * floppy, null, tty, etc.
                    402:  */
                    403: maybetty()
                    404: {
                    405:        register char *cp = dbuf.d_name;
                    406:        register struct ttys *dp;
                    407:        int x;
                    408:        struct stat stb;
                    409: 
                    410:        switch (cp[0]) {
                    411: 
                    412:        case 'c':
                    413:                if (!strcmp(cp, "console")) {
                    414:                        x = 0;
                    415:                        goto donecand;
                    416:                }
                    417:                /* cu[la]? are possible!?! don't rule them out */
                    418:                break;
                    419: 
                    420:        case 'd':
                    421:                if (!strcmp(cp, "drum"))
                    422:                        return (0);
                    423:                break;
                    424: 
                    425:        case 'f':
                    426:                if (!strcmp(cp, "floppy"))
                    427:                        return (0);
                    428:                break;
                    429: 
                    430:        case 'k':
                    431:                cp++;
                    432:                if (*cp == 'U')
                    433:                        cp++;
                    434:                goto trymem;
                    435: 
                    436:        case 'r':
                    437:                cp++;
                    438:                if (*cp == 'r' || *cp == 'u' || *cp == 'h')
                    439:                        cp++;
                    440: #define is(a,b) cp[0] == 'a' && cp[1] == 'b'
                    441:                if (is(r,p) || is(u,p) || is(r,k) || is(r,m) || is(m,t)) {
                    442:                        cp += 2;
                    443:                        if (isdigit(*cp) && cp[2] == 0)
                    444:                                return (0);
                    445:                }
                    446:                break;
                    447: 
                    448:        case 'm':
                    449: trymem:
                    450:                if (cp[0] == 'm' && cp[1] == 'e' && cp[2] == 'm' && cp[3] == 0)
                    451:                        return (0);
                    452:                break;
                    453: 
                    454:        case 'n':
                    455:                if (!strcmp(cp, "null"))
                    456:                        return (0);
                    457:                break;
                    458: 
                    459:        case 'v':
                    460:                if ((cp[1] == 'a' || cp[1] == 'p') && isdigit(cp[2]) &&
                    461:                    cp[3] == 0)
                    462:                        return (0);
                    463:                break;
                    464:        }
                    465: mightbe:
                    466:        cp = dbuf.d_name;
                    467:        while (cp < &dbuf.d_name[DIRSIZ] && *cp)
                    468:                cp++;
                    469:        --cp;
                    470:        x = 0;
                    471:        if (cp[-1] == 'd') {
                    472:                if (dialbase == -1) {
                    473:                        if (stat("ttyd0", &stb) == 0)
                    474:                                dialbase = stb.st_rdev & 017;
                    475:                        else
                    476:                                dialbase = -2;
                    477:                }
                    478:                if (dialbase == -2)
                    479:                        x = 0;
                    480:                else
                    481:                        x = 11;
                    482:        }
                    483:        if (cp > dbuf.d_name && isdigit(cp[-1]) && isdigit(*cp))
                    484:                x += 10 * (cp[-1] - ' ') + cp[0] - '0';
                    485:        else if (*cp >= 'a' && *cp <= 'f')
                    486:                x += 10 + *cp - 'a';
                    487:        else if (isdigit(*cp))
                    488:                x += *cp - '0';
                    489:        else
                    490:                x = -1;
                    491: donecand:
                    492:        dp = (struct ttys *)alloc(sizeof (struct ttys));
                    493:        strncpy(dp->name, dbuf.d_name, DIRSIZ);
                    494:        dp->next = allttys;
                    495:        dp->ttyd = -1;
                    496:        allttys = dp;
                    497:        if (x == -1)
                    498:                return;
                    499:        x &= 017;
                    500:        dp->cand = cand[x];
                    501:        cand[x] = dp;
                    502: }
                    503: 
                    504: char *
                    505: gettty()
                    506: {
                    507:        register char *p;
                    508:        register struct ttys *dp;
                    509:        struct stat stb;
                    510:        int x;
                    511: 
                    512:        if (u.u_ttyp == 0)
                    513:                return("?");
                    514:        x = u.u_ttyd & 017;
                    515:        for (dp = cand[x]; dp; dp = dp->cand) {
                    516:                if (dp->ttyd == -1) {
                    517:                        if (stat(dp->name, &stb) == 0 &&
                    518:                           (stb.st_mode&S_IFMT)==S_IFCHR)
                    519:                                dp->ttyd = stb.st_rdev;
                    520:                        else
                    521:                                dp->ttyd = -2;
                    522:                }
                    523:                if (dp->ttyd == u.u_ttyd)
                    524:                        goto found;
                    525:        }
                    526:        /* ick */
                    527:        for (dp = allttys; dp; dp = dp->next) {
                    528:                if (dp->ttyd == -1) {
                    529:                        if (stat(dp->name, &stb) == 0)
                    530:                                dp->ttyd = stb.st_rdev;
                    531:                        else
                    532:                                dp->ttyd = -2;
                    533:                }
                    534:                if (dp->ttyd == u.u_ttyd)
                    535:                        goto found;
                    536:        }
                    537:        return ("?");
                    538: found:
                    539:        p = dp->name;
                    540:        if (p[0]=='t' && p[1]=='t' && p[2]=='y')
                    541:                p += 3;
                    542:        return (p);
                    543: }
                    544: 
                    545: save()
                    546: {
                    547:        register struct savcom *sp;
                    548:        register struct asav *ap;
                    549:        register char *cp;
                    550:        register struct text *xp;
                    551:        char *ttyp, *cmdp;
                    552: 
                    553:        if (mproc->p_stat != SZOMB && getu() == 0)
                    554:                return;
                    555:        ttyp = gettty();
                    556:        if (xflg == 0 && ttyp[0] == '?' || tptr && strcmpn(tptr, ttyp, 2))
                    557:                return;
                    558:        sp = &savcom[npr];
                    559:        cmdp = getcmd();
                    560:        if (cmdp == 0)
                    561:                return;
                    562:        sp->ap = ap = (struct asav *)alloc(sizeof (struct asav));
                    563:        sp->ap->a_cmdp = cmdp;
                    564: #define e(a,b) ap->a = mproc->b
                    565:        e(a_flag, p_flag); e(a_stat, p_stat); e(a_nice, p_nice);
                    566:        e(a_uid, p_uid); e(a_pid, p_pid); e(a_pri, p_pri);
                    567:        e(a_slptime, p_slptime); e(a_time, p_time);
                    568:        ap->a_tty[0] = ttyp[0];
                    569:        ap->a_tty[1] = ttyp[1] ? ttyp[1] : ' ';
                    570:        if (ap->a_stat == SZOMB) {
                    571:                register struct xproc *xp = (struct xproc *)mproc;
                    572: 
                    573:                ap->a_cpu = xp->xp_vm.vm_utime + xp->xp_vm.vm_stime;
                    574:        } else {
                    575:                ap->a_size = mproc->p_dsize + mproc->p_ssize;
                    576:                e(a_rss, p_rssize); 
                    577:                ap->a_ttyd = u.u_ttyd;
                    578:                ap->a_cpu = u.u_vm.vm_utime + u.u_vm.vm_stime;
                    579:                if (sumcpu)
                    580:                        ap->a_cpu += u.u_cvm.vm_utime + u.u_cvm.vm_stime;
                    581:                if (mproc->p_textp && text) {
                    582:                        xp = &text[mproc->p_textp -
                    583:                                    (struct text *)nl[X_TEXT].n_value];
                    584:                        ap->a_tsiz = xp->x_size;
                    585:                        ap->a_txtrss = xp->x_rssize;
                    586:                        ap->a_xccount = xp->x_ccount;
                    587:                }
                    588:        }
                    589: #undef e
                    590:        ap->a_cpu /= HZ;
                    591:        if (lflg) {
                    592:                register struct lsav *lp;
                    593: 
                    594:                sp->sun.lp = lp = (struct lsav *)alloc(sizeof (struct lsav));
                    595: #define e(a,b) lp->a = mproc->b
                    596:                e(l_ppid, p_ppid); e(l_cpu, p_cpu);
                    597:                if (ap->a_stat != SZOMB)
                    598:                        e(l_wchan, p_wchan);
                    599: #undef e
                    600:                lp->l_addr = pcbpf;
                    601:        } else if (vflg) {
                    602:                register struct vsav *vp;
                    603: 
                    604:                sp->sun.vp = vp = (struct vsav *)alloc(sizeof (struct vsav));
                    605: #define e(a,b) vp->a = mproc->b
                    606:                if (ap->a_stat != SZOMB) {
                    607:                        e(v_swrss, p_swrss);
                    608:                        vp->v_majflt = u.u_vm.vm_majflt;
                    609:                        if (mproc->p_textp)
                    610:                                vp->v_txtswrss = xp->x_swrss;
                    611:                }
                    612:                vp->v_pctcpu = pcpu();
                    613: #undef e
                    614:        } else if (uflg)
                    615:                sp->sun.u_pctcpu = pcpu();
                    616:        else if (sflg) {
                    617:                if (ap->a_stat != SZOMB) {
                    618:                        for (cp = (char *)u.u_stack;
                    619:                            cp < &user.upages[UPAGES][NBPG]; )
                    620:                                if (*cp++)
                    621:                                        break;
                    622:                        sp->sun.s_ssiz = (&user.upages[UPAGES][NBPG] - cp);
                    623:                }
                    624:        }
                    625:        npr++;
                    626: }
                    627: 
                    628: double
                    629: pmem(ap)
                    630:        register struct asav *ap;
                    631: {
                    632:        double fracmem;
                    633:        int szptudot;
                    634: 
                    635:        if ((ap->a_flag&SLOAD) == 0)
                    636:                fracmem = 0.0;
                    637:        else {
                    638:                szptudot = UPAGES + clrnd(ctopt(ap->a_size+ap->a_tsiz));
                    639:                fracmem = ((float)ap->a_rss+szptudot)/CLSIZE/ecmx;
                    640:                if (ap->a_xccount)
                    641:                        fracmem += ((float)ap->a_txtrss)/CLSIZE/
                    642:                            ap->a_xccount/ecmx;
                    643:        }
                    644:        return (100.0 * fracmem);
                    645: }
                    646: 
                    647: double
                    648: pcpu()
                    649: {
                    650:        time_t time;
                    651: 
                    652:        time = mproc->p_time;
                    653:        if (time == 0 || (mproc->p_flag&SLOAD) == 0)
                    654:                return (0.0);
                    655:        if (rawcpu)
                    656:                return (100.0 * mproc->p_pctcpu);
                    657:        return (100.0 * mproc->p_pctcpu / (1.0 - exp(time * log(ccpu))));
                    658: }
                    659: 
                    660: getu()
                    661: {
                    662:        struct pte *pteaddr, apte;
                    663:        int pad1;       /* avoid hardware botch */
                    664:        struct pte arguutl[UPAGES+CLSIZE];
                    665:        int pad2;       /* avoid hardware botch */
                    666:        register int i;
                    667:        int ncl, size;
                    668: 
                    669:        size = sflg ? ctob(UPAGES) : sizeof (struct user);
                    670:        if ((mproc->p_flag & SLOAD) == 0) {
                    671:                lseek(swap, ctob(mproc->p_swaddr), 0);
                    672:                if (read(swap, (char *)&user.user, size) != size) {
                    673:                        fprintf(stderr, "ps: cant read u for pid %d from %s\n",
                    674:                            mproc->p_pid, swapf);
                    675:                        return (0);
                    676:                }
                    677:                pcbpf = 0;
                    678:                argaddr = 0;
                    679:                return (1);
                    680:        }
                    681:        pteaddr = &Usrptma[btokmx(mproc->p_p0br) + mproc->p_szpt - 1];
                    682:        lseek(kmem, kflg ? clear(pteaddr) : (int)pteaddr, 0);
                    683:        if (read(kmem, (char *)&apte, sizeof(apte)) != sizeof(apte)) {
                    684:                printf("ps: cant read indir pte to get u for pid %d from %s\n",
                    685:                    mproc->p_pid, swapf);
                    686:                return (0);
                    687:        }
                    688:        lseek(mem,
                    689:            ctob(apte.pg_pfnum+1) - (UPAGES+CLSIZE) * sizeof (struct pte), 0);
                    690:        if (read(mem, (char *)arguutl, sizeof(arguutl)) != sizeof(arguutl)) {
                    691:                printf("ps: cant read page table for u of pid %d from %s\n",
                    692:                    mproc->p_pid, swapf);
                    693:                return (0);
                    694:        }
                    695:        if (arguutl[0].pg_fod == 0 && arguutl[0].pg_pfnum)
                    696:                argaddr = ctob(arguutl[0].pg_pfnum);
                    697:        else
                    698:                argaddr = 0;
                    699:        pcbpf = arguutl[CLSIZE].pg_pfnum;
                    700:        ncl = (size + NBPG*CLSIZE - 1) / (NBPG*CLSIZE);
                    701:        while (--ncl >= 0) {
                    702:                i = ncl * CLSIZE;
                    703:                lseek(mem, ctob(arguutl[CLSIZE+i].pg_pfnum), 0);
                    704:                if (read(mem, user.upages[i], CLSIZE*NBPG) != CLSIZE*NBPG) {
                    705:                        printf("ps: cant read page %d of u of pid %d from %s\n",
                    706:                            arguutl[CLSIZE+i].pg_pfnum, mproc->p_pid, memf);
                    707:                        return(0);
                    708:                }
                    709:        }
                    710:        return (1);
                    711: }
                    712: 
                    713: char *
                    714: getcmd()
                    715: {
                    716:        char cmdbuf[BUFSIZ];
                    717:        int pad1;               /* avoid hardware botch */
                    718:        union {
                    719:                char    argc[CLSIZE*NBPG];
                    720:                int     argi[CLSIZE*NBPG/sizeof (int)];
                    721:        } argspac;
                    722:        int pad2;               /* avoid hardware botch */
                    723:        register char *cp;
                    724:        register int *ip;
                    725:        char c;
                    726:        int nbad;
                    727:        struct dblock db;
                    728: 
                    729:        if (mproc->p_stat == SZOMB || mproc->p_flag&(SSYS|SWEXIT))
                    730:                return ("");
                    731:        if (cflg) {
                    732:                strncpy(cmdbuf, u.u_comm, sizeof (u.u_comm));
                    733:                return (savestr(cmdbuf));
                    734:        }
                    735:        if ((mproc->p_flag & SLOAD) == 0 || argaddr == 0) {
                    736:                vstodb(0, CLSIZE, &u.u_smap, &db, 1);
                    737:                lseek(swap, ctob(db.db_base), 0);
                    738:                if (read(swap, (char *)&argspac, sizeof(argspac))
                    739:                    != sizeof(argspac))
                    740:                        goto bad;
                    741:        } else {
                    742:                lseek(mem, argaddr, 0);
                    743:                if (read(mem, (char *)&argspac, sizeof (argspac))
                    744:                    != sizeof (argspac))
                    745:                        goto bad;
                    746:        }
                    747:        ip = &argspac.argi[CLSIZE*NBPG/sizeof (int)];
                    748:        ip -= 2;                /* last arg word and .long 0 */
                    749:        while (*--ip)
                    750:                if (ip == argspac.argi)
                    751:                        goto retucomm;
                    752:        *(char *)ip = ' ';
                    753:        ip++;
                    754:        nbad = 0;
                    755:        for (cp = (char *)ip; cp < &argspac.argc[CLSIZE*NBPG]; cp++) {
                    756:                c = *cp & 0177;
                    757:                if (c == 0)
                    758:                        *cp = ' ';
                    759:                else if (c < ' ' || c > 0176) {
                    760:                        if (++nbad >= 5*(eflg+1)) {
                    761:                                *cp++ = ' ';
                    762:                                break;
                    763:                        }
                    764:                        *cp = '?';
                    765:                } else if (eflg == 0 && c == '=') {
                    766:                        while (*--cp != ' ')
                    767:                                if (cp <= (char *)ip)
                    768:                                        break;
                    769:                        break;
                    770:                }
                    771:        }
                    772:        *cp = 0;
                    773:        while (*--cp == ' ')
                    774:                *cp = 0;
                    775:        cp = (char *)ip;
                    776:        strncpy(cmdbuf, cp, &argspac.argc[CLSIZE*NBPG] - cp);
                    777:        if (cp[0] == '-' || cp[0] == '?' || cp[0] <= ' ') {
                    778:                strcat(cmdbuf, " (");
                    779:                strncat(cmdbuf, u.u_comm, sizeof(u.u_comm));
                    780:                strcat(cmdbuf, ")");
                    781:        }
                    782: /*
                    783:        if (xflg == 0 && gflg == 0 && tptr == 0 && cp[0] == '-')
                    784:                return (0);
                    785: */
                    786:        return (savestr(cmdbuf));
                    787: 
                    788: bad:
                    789:        fprintf(stderr, "ps: error locating command name for pid %d\n",
                    790:            mproc->p_pid);
                    791: retucomm:
                    792:        strcpy(cmdbuf, " (");
                    793:        strncat(cmdbuf, u.u_comm, sizeof (u.u_comm));
                    794:        strcat(cmdbuf, ")");
                    795:        return (savestr(cmdbuf));
                    796: }
                    797: 
                    798: char   *lhdr =
                    799: "     F UID   PID  PPID CP PRI NI ADDR  SZ  RSS WCHAN STAT TT  TIME";
                    800: lpr(sp)
                    801:        struct savcom *sp;
                    802: {
                    803:        register struct asav *ap = sp->ap;
                    804:        register struct lsav *lp = sp->sun.lp;
                    805: 
                    806:        printf("%6x%4d%6u%6u%3d%4d%3d%5x%4d%5d",
                    807:            ap->a_flag, ap->a_uid,
                    808:            ap->a_pid, lp->l_ppid, lp->l_cpu&0377, ap->a_pri-PZERO,
                    809:            ap->a_nice-NZERO, lp->l_addr, ap->a_size/2, ap->a_rss/2);
                    810:        printf(lp->l_wchan ? " %5x" : "      ", (int)lp->l_wchan&0xfffff);
                    811:        printf(" %4.4s ", state(ap));
                    812:        ptty(ap->a_tty);
                    813:        ptime(ap);
                    814: }
                    815: 
                    816: ptty(tp)
                    817:        char *tp;
                    818: {
                    819: 
                    820:        printf("%-2.2s", tp);
                    821: }
                    822: 
                    823: ptime(ap)
                    824:        struct asav *ap;
                    825: {
                    826: 
                    827:        printf("%3ld:%02ld", ap->a_cpu / HZ, ap->a_cpu % HZ);
                    828: }
                    829: 
                    830: char   *uhdr =
                    831: "USER       PID %CPU %MEM   SZ  RSS TT STAT  TIME";
                    832: upr(sp)
                    833:        struct savcom *sp;
                    834: {
                    835:        register struct asav *ap = sp->ap;
                    836:        int vmsize, rmsize;
                    837: 
                    838:        vmsize = (ap->a_size + ap->a_tsiz)/2;
                    839:        rmsize = ap->a_rss/2;
                    840:        if (ap->a_xccount)
                    841:                rmsize += ap->a_txtrss/ap->a_xccount/2;
                    842:        printf("%-8.8s %5d%5.1f%5.1f%5d%5d",
                    843:            getname(ap->a_uid), ap->a_pid, sp->sun.u_pctcpu, pmem(ap),
                    844:            vmsize, rmsize);
                    845:        putchar(' ');
                    846:        ptty(ap->a_tty);
                    847:        printf(" %4.4s", state(ap));
                    848:        ptime(ap);
                    849: }
                    850: 
                    851: char *vhdr =
                    852: "  PID TT STAT  TIME SL RE PAGEIN SIZE  RSS  SRS TSIZ TRS %CPU %MEM";
                    853: vpr(sp)
                    854:        struct savcom *sp;
                    855: {
                    856:        register struct vsav *vp = sp->sun.vp;
                    857:        register struct asav *ap = sp->ap;
                    858: 
                    859:        printf("%5u ", ap->a_pid);
                    860:        ptty(ap->a_tty);
                    861:        printf(" %4.4s", state(ap));
                    862:        ptime(ap);
                    863:        printf("%3d%3d%7d%5d%5d%5d%5d%4d%5.1f%5.1f",
                    864:           ap->a_slptime, ap->a_time > 99 ? 99 : ap->a_time, vp->v_majflt,
                    865:           ap->a_size/2, ap->a_rss/2, vp->v_swrss/2,
                    866:           ap->a_tsiz/2, ap->a_txtrss/2, vp->v_pctcpu, pmem(ap));
                    867: }
                    868: 
                    869: char   *shdr =
                    870: "SSIZ   PID TT STAT  TIME";
                    871: spr(sp)
                    872:        struct savcom *sp;
                    873: {
                    874:        register struct asav *ap = sp->ap;
                    875: 
                    876:        if (sflg)
                    877:                printf("%4d ", sp->sun.s_ssiz);
                    878:        printf("%5u", ap->a_pid);
                    879:        putchar(' ');
                    880:        ptty(ap->a_tty);
                    881:        printf(" %4.4s", state(ap));
                    882:        ptime(ap);
                    883: }
                    884: 
                    885: char *
                    886: state(ap)
                    887:        register struct asav *ap;
                    888: {
                    889:        char stat, load, nice, anom;
                    890:        static char res[5];
                    891: 
                    892:        switch (ap->a_stat) {
                    893: 
                    894:        case SSTOP:
                    895:                stat = 'T';
                    896:                break;
                    897: 
                    898:        case SSLEEP:
                    899:                if (ap->a_pri >= PZERO)
                    900:                        if (ap->a_slptime >= MAXSLP)
                    901:                                stat = 'I';
                    902:                        else
                    903:                                stat = 'S';
                    904:                else if (ap->a_flag & SPAGE)
                    905:                        stat = 'P';
                    906:                else
                    907:                        stat = 'D';
                    908:                break;
                    909: 
                    910:        case SWAIT:
                    911:        case SRUN:
                    912:        case SIDL:
                    913:                stat = 'R';
                    914:                break;
                    915: 
                    916:        case SZOMB:
                    917:                stat = 'Z';
                    918:                break;
                    919: 
                    920:        default:
                    921:                stat = '?';
                    922:        }
                    923:        load = ap->a_flag & SLOAD ? ' ' : 'W';
                    924:        if (ap->a_nice < NZERO)
                    925:                nice = '<';
                    926:        else if (ap->a_nice > NZERO)
                    927:                nice = 'N';
                    928:        else
                    929:                nice = ' ';
                    930:        anom = ap->a_flag & (SANOM|SUANOM) ? 'A' : ' ';
                    931:        res[0] = stat; res[1] = load; res[2] = nice; res[3] = anom;
                    932:        return (res);
                    933: }
                    934: 
                    935: /*
                    936:  * Given a base/size pair in virtual swap area,
                    937:  * return a physical base/size pair which is the
                    938:  * (largest) initial, physically contiguous block.
                    939:  */
                    940: vstodb(vsbase, vssize, dmp, dbp, rev)
                    941:        register int vsbase;
                    942:        int vssize;
                    943:        struct dmap *dmp;
                    944:        register struct dblock *dbp;
                    945: {
                    946:        register int blk = DMMIN;
                    947:        register swblk_t *ip = dmp->dm_map;
                    948: 
                    949:        if (vsbase < 0 || vsbase + vssize > dmp->dm_size)
                    950:                panic("vstodb");
                    951:        while (vsbase >= blk) {
                    952:                vsbase -= blk;
                    953:                if (blk < DMMAX)
                    954:                        blk *= 2;
                    955:                ip++;
                    956:        }
                    957:        if (*ip <= 0 || *ip + blk > nswap)
                    958:                panic("vstodb *ip");
                    959:        dbp->db_size = min(vssize, blk - vsbase);
                    960:        dbp->db_base = *ip + (rev ? blk - (vsbase + dbp->db_size) : vsbase);
                    961: }
                    962: 
                    963: /*ARGSUSED*/
                    964: panic(cp)
                    965:        char *cp;
                    966: {
                    967: 
                    968: #ifdef DEBUG
                    969:        printf("%s\n", cp);
                    970: #endif
                    971: }
                    972: 
                    973: min(a, b)
                    974: {
                    975: 
                    976:        return (a < b ? a : b);
                    977: }
                    978: 
                    979: pscomp(s1, s2)
                    980:        struct savcom *s1, *s2;
                    981: {
                    982:        register int i;
                    983: 
                    984:        if (uflg)
                    985:                return (s2->sun.u_pctcpu > s1->sun.u_pctcpu ? 1 : -1);
                    986:        if (vflg)
                    987:                return (vsize(s2) - vsize(s1));
                    988:        i = s1->ap->a_ttyd - s2->ap->a_ttyd;
                    989:        if (i == 0)
                    990:                i = s1->ap->a_pid - s2->ap->a_pid;
                    991:        return (i);
                    992: }
                    993: 
                    994: vsize(sp)
                    995:        struct savcom *sp;
                    996: {
                    997:        register struct asav *ap = sp->ap;
                    998:        register struct vsav *vp = sp->sun.vp;
                    999:        
                   1000:        if (ap->a_flag & SLOAD)
                   1001:                return (ap->a_rss +
                   1002:                    ap->a_txtrss / (ap->a_xccount ? ap->a_xccount : 1));
                   1003:        return (vp->v_swrss + (ap->a_xccount ? 0 : vp->v_txtswrss));
                   1004: }
                   1005: 
                   1006: #define        NMAX    8
                   1007: #define        NUID    2048
                   1008: 
                   1009: char   names[NUID][NMAX+1];
                   1010: 
                   1011: /*
                   1012:  * Stolen from ls...
                   1013:  */
                   1014: char *
                   1015: getname(uid)
                   1016: {
                   1017:        register struct passwd *pw;
                   1018:        static init;
                   1019:        struct passwd *getpwent();
                   1020: 
                   1021:        if (uid >= 0 && uid < NUID && names[uid][0])
                   1022:                return (&names[uid][0]);
                   1023:        if (init == 2)
                   1024:                return (0);
                   1025:        if (init == 0)
                   1026:                setpwent(), init = 1;
                   1027:        while (pw = getpwent()) {
                   1028:                if (pw->pw_uid >= NUID)
                   1029:                        continue;
                   1030:                if (names[pw->pw_uid][0])
                   1031:                        continue;
                   1032:                strncpy(names[pw->pw_uid], pw->pw_name, NMAX);
                   1033:                if (pw->pw_uid == uid)
                   1034:                        return (&names[uid][0]);
                   1035:        }
                   1036:        init = 2;
                   1037:        endpwent();
                   1038:        return (0);
                   1039: }
                   1040: 
                   1041: char   *freebase;
                   1042: int    nleft;
                   1043: 
                   1044: char *
                   1045: alloc(size)
                   1046:        int size;
                   1047: {
                   1048:        register char *cp;
                   1049:        register int i;
                   1050: 
                   1051:        if (size > nleft) {
                   1052:                freebase = (char *)sbrk(i = size > 2048 ? size : 2048);
                   1053:                if (freebase == 0) {
                   1054:                        fprintf(stderr, "ps: ran out of memory\n");
                   1055:                        exit(1);
                   1056:                }
                   1057:                nleft = i - size;
                   1058:        } else
                   1059:                nleft -= size;
                   1060:        cp = freebase;
                   1061:        for (i = size; --i >= 0; )
                   1062:                *cp++ = 0;
                   1063:        freebase = cp;
                   1064:        return (cp - size);
                   1065: }
                   1066: 
                   1067: char *
                   1068: savestr(cp)
                   1069:        char *cp;
                   1070: {
                   1071:        register int len;
                   1072:        register char *dp;
                   1073: 
                   1074:        len = strlen(cp);
                   1075:        dp = (char *)alloc(len+1);
                   1076:        strcpy(dp, cp);
                   1077:        return (dp);
                   1078: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.