📄 sa.c
字号:
printf("*ignore*re/cp");/* aki00 end */ if(dflg || !Dflg) printf("%10.0favio", e/(n?n:1)); else printf("%10.0ftio", e); if (kflg || !Kflg) printf("%10.0fk", d/(SCALEF*((b+c)!=0.0?(b+c):1.0))); else printf("%10.0fk*sec", d/(SCALEF*AHZ));}static col(n, a, m, cp) double n, a, m; char *cp;{ if(jflg) printf("%11.2f%s", a/(n*(double)AHZ), cp); else printf("%11.2f%s", a/(60.*(double)AHZ), cp); if(cflg) { if(a == m) printf("%9s", ""); else printf("%8.2f%%", 100.*a/m); }}static doacct(f)char *f;{ FILE *ff; double x, y, z; /*RR*/ struct acct fbuf; register char *cp; register int c; register struct user *up; register cell *tp;#ifdef DEBUG int nrecords = 0;#endif DEBUG if (sflg && sname) { printf("Only 1 file with -s\n"); exit(0); } if (sflg) sname = f; if ((ff = fopen(f, "r"))==NULL) { printf("Can't open %s\n", f); return; }/* aps01 */ if (uflg) getnames(); while (fread((char *)&fbuf, sizeof(fbuf), 1, ff) == 1) {#ifdef DEBUG if (++nrecords % 1000 == 0) printf("Input record from %s number %d\n", f, nrecords);#endif DEBUG /* * Error checking - let's be sure times are non-negative * Setting or getting bogus dates with "date" can cause * negative times */#ifdef vax /* JLR001 */ if (fbuf.ac_utime<0.0 || fbuf.ac_stime<0.0 || fbuf.ac_mem<0.0 || fbuf.ac_io<0.0 || fbuf.ac_etime<0.0) continue; /*RR*/#else if ((signed)fbuf.ac_utime < 0 || (signed)fbuf.ac_stime<0 || (signed)fbuf.ac_mem<0 || (signed)fbuf.ac_io<0 || (signed)fbuf.ac_etime<0) continue;#endif if (fbuf.ac_comm[0]==0) { fbuf.ac_comm[0] = '?'; } for (cp = fbuf.ac_comm; cp < &fbuf.ac_comm[NC]; cp++) { c = *cp & 0377; if (c && (c < ' ' || c >= 0200)) *cp = '?'; else/* aps 00 */ *cp = c ; } if (fbuf.ac_flag&AFORK) { for (cp=fbuf.ac_comm; cp < &fbuf.ac_comm[NC]; cp++) if (*cp==0) {/* aki 02 */ *cp++ = '*'; if (cp < &fbuf.ac_comm[NC]) *cp = 0;/* aki 02 end */ break; } }#ifdef vax /* JLR001 */ x = fbuf.ac_utime + fbuf.ac_stime; y = fbuf.ac_mem; z = fbuf.ac_io;#else x = expand(fbuf.ac_utime) + expand(fbuf.ac_stime); y = pgtok((u_short)fbuf.ac_mem); z = expand(fbuf.ac_io) / (double)AHZ;#endif if (uflg) { struct user *us;/* aps02 */ us = wasuser(fbuf.ac_uid); if (us->us_name[0]) printf("%-8s ", us->us_name); else printf("%-8d ", fbuf.ac_uid);/* aps02 end */#ifndef vax /* JLR001 */ printf("%6.2f cpu %8luk mem %6ld io %.*s\n", x/(double)AHZ, (int)y, (int)z, NC, fbuf.ac_comm);#else vax/* aki01 */ printf("%9.2lfcp ", x); /*RR*/ printf("%9.2lfmem ", y); /*RR*/ printf("%7.0lfio ", z); /*RR*/ printf("%.10s\n", fbuf.ac_comm);/* aki01 end */#endif !vax continue; } up = finduser(fbuf.ac_uid); if (up == 0) continue; /* preposterous user id */ up->us_cnt++;#ifdef vax /* JLR001 */ up->us_ctime += x;#else up->us_ctime += x/(double)AHZ;#endif up->us_imem += x * y; up->us_io += z; ncom += 1.0; tp = enter(fbuf.ac_comm);/* RR begin */ tp->p.imem += x * y; timem += x * y; tp->p.count++;#ifdef vax /* JLR001 */ tp->p.realt += fbuf.ac_etime; treal += fbuf.ac_etime; tp->p.cput += fbuf.ac_utime; tcpu += fbuf.ac_utime; tp->p.syst += fbuf.ac_stime; tsys += fbuf.ac_stime;#else x = expand(fbuf.ac_etime); tp->p.realt += x; treal += x; x = expand(fbuf.ac_utime); tp->p.cput += x; tcpu += x; x = expand(fbuf.ac_stime); tp->p.syst += x; tsys += x;#endif/* RR end */ tp->p.io += z; tio += z; } fclose(ff);}/* * Generalized cell compare routine, to cast out users */static cellcmp(p1, p2) cell *p1, *p2;{ if (ISPROCESS(p1)){ if (ISPROCESS(p2)) return((*cmp)(p1, p2)); return(-1); } if (ISPROCESS(p2)) return(1); return(0);}static ncmp(p1, p2) cell *p1, *p2;{ if(p1->p.count == p2->p.count) return(tcmp(p1, p2)); if(rflg) return(p1->p.count - p2->p.count); return(p2->p.count - p1->p.count);}static bcmp(p1, p2) cell *p1, *p2;{ double f1, f2; double sum(); f1 = sum(p1)/p1->p.count; f2 = sum(p2)/p2->p.count; if(f1 < f2) { if(rflg) return(-1); return(1); } if(f1 > f2) { if(rflg) return(1); return(-1); } return(0);}static Kcmp(p1, p2) cell *p1, *p2;{ if (p1->p.imem < p2->p.imem) { if(rflg) return(-1); return(1); } if (p1->p.imem > p2->p.imem) { if(rflg) return(1); return(-1); } return(0);}static kcmp(p1, p2) cell *p1, *p2;{ double a1, a2; a1 = p1->p.imem / ((p1->p.cput+p1->p.syst)?(p1->p.cput+p1->p.syst):1); a2 = p2->p.imem / ((p2->p.cput+p2->p.syst)?(p2->p.cput+p2->p.syst):1); if (a1 < a2) { if(rflg) return(-1); return(1); } if (a1 > a2) { if(rflg) return(1); return(-1); } return(0);}static dcmp(p1, p2) cell *p1, *p2;{ double a1, a2; a1 = p1->p.io / (p1->p.count?p1->p.count:1); a2 = p2->p.io / (p2->p.count?p2->p.count:1); if (a1 < a2) { if(rflg) return(-1); return(1); } if (a1 > a2) { if(rflg) return(1); return(-1); } return(0);}static Dcmp(p1, p2) cell *p1, *p2;{ if (p1->p.io < p2->p.io) { if(rflg) return(-1); return(1); } if (p1->p.io > p2->p.io) { if(rflg) return(1); return(-1); } return(0);}static tcmp(p1, p2) cell *p1, *p2;{ extern double sum(); double f1, f2; f1 = sum(p1); f2 = sum(p2); if(f1 < f2) { if(rflg) return(-1); return(1); } if(f1 > f2) { if(rflg) return(1); return(-1); } return(0);}static double sum(p) cell *p;{ if(p->p.name[0] == 0) return(0.0); return( p->p.cput + p->p.syst);}static init(){ struct user userbuf; struct process tbuf; register cell *tp; register struct user *up; int uid; FILE *f; if ((f = fopen(SAVACCT, "r")) == NULL) goto gshm; while (fread((char *)&tbuf, sizeof(struct process), 1, f) == 1) { tp = enter(tbuf.name); ncom += tbuf.count; tp->p.count = tbuf.count; treal += tbuf.realt; tp->p.realt = tbuf.realt; tcpu += tbuf.cput; tp->p.cput = tbuf.cput; tsys += tbuf.syst; tp->p.syst = tbuf.syst; tio += tbuf.io; tp->p.io = tbuf.io; timem += tbuf.imem; tp->p.imem = tbuf.imem; } fclose(f); gshm: if ((f = fopen(USRACCT, "r")) == NULL) return; for(uid = 0; fread((char *)&(userbuf.oldu), sizeof(struct Olduser), 1, f) == 1; uid++){ if (userbuf.us_cnt){ up = finduser(uid); if (up == 0) continue; /* preposterous user id */ up->oldu = userbuf.oldu; } } fclose(f);}static strip(){ int c; register struct allocbox *allocwalk; register cell *tp, *ub, *junkp; if (fflg) printf("Categorizing commands used %d times or fewer as **junk**\n", thres); junkp = enter("**junk**"); PROCESSITERATE(allocwalk, tp, ub){ if (tp->p.name[0] && tp->p.count <= thres) { if (!fflg) printf("%.14s--", tp->p.name); if (fflg || ((c=getchar())=='y')) { tp->p.name[0] = '\0'; junkp->p.count += tp->p.count; junkp->p.realt += tp->p.realt; junkp->p.cput += tp->p.cput; junkp->p.syst += tp->p.syst; junkp->p.imem += tp->p.imem; junkp->p.io += tp->p.io; } if (!fflg) while (c && c!='\n') c = getchar(); } }}static char UserKey[NAMELG + 2];#ifndef vax /* JLR001 */static time_texpand(t) unsigned t;{ register time_t nt; nt = t&017777; t >>= 13; while (t!=0) { t--; nt <<= 3; } return(nt);}#endif !vaxstatic char *makekey(uid) int uid;{ (void)sprintf(UserKey+1, "%04x", uid); UserKey[0] = USERKEY; return(UserKey);}static struct user *wasuser(uid) int uid;{ struct user *tp; htabinstall = 0; tp = finduser(uid); htabinstall = 1; return(tp);}/* * Only call this if you really want to insert it in the table! */static struct user *finduser(uid) int uid;{ if (uid > maxuser){ fprintf(stderr, "Preposterous user id, %d: ignored\n", uid); return(0); } return((struct user*)enter(makekey(uid)));}/* * Set the names of all users in the password file. * We will later not print those that didn't do anything. */static getnames(){ register struct user *tp; register struct passwd *pw; struct passwd *getpwent(); int htabinstall_save; htabinstall_save = htabinstall; /* RR save the install flag */ setpwent(); while (pw = getpwent()){ if ( (tp = wasuser(pw->pw_uid)) != 0 && tp->us_name[0] != NULL) continue; /*RR*/ /* check to see if the uid and name is in there already */ /* if so, go to next entry */ /* if it wasn't do the following which installs it */ htabinstall = 1; if ( (tp = finduser(pw->pw_uid)) != 0) { strncpy(tp->us_name, pw->pw_name, NAMELG); } htabinstall = 0; } endpwent(); htabinstall = htabinstall_save; /* RR set flag back */}static intgetmaxuid(){ register struct user *tp; register struct passwd *pw; struct passwd *getpwent(); int maxuid = -1; setpwent(); while(pw = getpwent()){ if (pw->pw_uid > maxuid) maxuid = pw->pw_uid; } endpwent(); return(maxuid);}static tabinit(){ allochead = 0; alloctail = 0; nexttab = 0; tabsleft = 0; htab = 0; ntabs = 0; htaballoc(); /* get the first part of the hash table */}#define ALLOCQTY sizeof (struct allocbox)static cell *taballoc(){ if (tabsleft == 0){ newbox = (struct allocbox *)calloc(1, ALLOCQTY); tabsleft = TABDALLOP; nexttab = &newbox->tabslots[0]; if (alloctail == 0){ allochead = alloctail = newbox; } else { alloctail->nextalloc = newbox; alloctail = newbox; } } --tabsleft; ++ntabs;#ifdef DEBUG if (ntabs % 100 == 0) printf("##Accounting table slot # %d\n", ntabs);#endif DEBUG return(nexttab++);}static htaballoc(){ register struct hashdallop *new;#ifdef DEBUG static int ntables = 0; printf("%%%New hash table chunk allocated, number %d\n", ++ntables);#endif DEBUG new = (struct hashdallop *)calloc(1, sizeof (struct hashdallop)); if (htab == 0) htab = new; else { /* add AFTER the 1st slot */ new->h_next = htab->h_next; htab->h_next = new; }}#define HASHCLOGGED (NHASH / 2)/* * Lookup a symbol passed in as the argument. * * We take pains to avoid function calls; this function * is called quite frequently, and the calling overhead * contributes significantly to the overall execution speed of sa. */static cell *enter(name) char *name; { static int initialprobe; register cell **hp; register char *from, *to; register int len, nprobes; static struct hashdallop *hdallop, *emptyhd; static cell **emptyslot, **hp_ub; emptyslot = 0; for (nprobes = 0, from = name, len = 0; *from && len < NC; nprobes <<= 2, nprobes += *from++, len++) continue; nprobes += from[-1] << 5; nprobes %= NHASH; if (nprobes < 0) nprobes += NHASH; initialprobe = nprobes; for (hdallop = htab; hdallop != 0; hdallop = hdallop->h_next){ for (hp = &(hdallop->h_tab[initialprobe]), nprobes = 1, hp_ub = &(hdallop->h_tab[NHASH]); (*hp) && (nprobes < NHASH); hp += nprobes, hp -= (hp >= hp_ub) ? NHASH:0, nprobes += 2) { from = name; to = (*hp)->p.name; for (len = 0; (len<NC) && *from; len++) if (*from++ != *to++) goto nextprobe; if (len >= NC) /*both are maximal length*/ return(*hp); if (*to == 0) /*assert *from == 0*/ return(*hp); nextprobe: ; } if (*hp == 0 && emptyslot == 0 && hdallop->h_nused < HASHCLOGGED) { emptyslot = hp; emptyhd = hdallop; } } if (emptyslot == 0) { htaballoc(); hdallop = htab->h_next; /* aren't we smart! */ hp = &hdallop->h_tab[initialprobe]; } else { hdallop = emptyhd; hp = emptyslot; } if (htabinstall){ *hp = taballoc(); hdallop->h_nused++; for(len = 0, from = name, to = (*hp)->p.name; (len<NC); len++) if ((*to++ = *from++) == '\0') break; return(*hp); } return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -