📄 m_irix62.c
字号:
if (pp->pr_state != 0 && (show_system || ((pp->pr_flag & SSYS) == 0))) { total_procs++; process_states[pp->pr_state]++; if ((!pp->pr_zomb) && (show_idle || (pp->pr_state == SRUN)) && (!show_uid || pp->pr_uid == (uid_t) sel->uid)) { *prefp++ = pp; active_procs++; } } } /* if requested, sort the "interesting" processes */ if (compare != NULL) qsort ((char *) pref, active_procs, sizeof (struct prpsinfo *), compare); /* remember active and total counts */ si->p_total = total_procs; si->p_active = active_procs; /* pass back a handle */ handle.next_proc = pref; handle.remaining = active_procs; return((caddr_t)&handle);}char *format_next_process(handle, get_userid) caddr_t handle; char *(*get_userid)();{ register struct prpsinfo *pp; struct handle *hp; register long cputime; register double pctcpu; /* find and remember the next proc structure */ hp = (struct handle *) handle; pp = *(hp->next_proc++); hp->remaining--; /* get the cpu usage and calculate the cpu percentages */ cputime = pp->pr_time.tv_sec; pctcpu = percent_cpu (pp); if (numcpus > 1) { if (pp->pr_sonproc < 0) state_abbrev[SRUN][4] = '*'; else state_abbrev[SRUN][4] = pp->pr_sonproc + '0'; } /* format this entry */ sprintf (fmt, Proc_format, pp->pr_pid, (*get_userid) (pp->pr_uid), pp->pr_pri - PZERO, pp->pr_nice - NZERO, format_k(pagetok(pp->pr_size)), format_k(pagetok(pp->pr_rssize)), state_abbrev[pp->pr_state], format_time(cputime), weighted_cpu (pp), pctcpu, pp->pr_fname); /* return the result */ return(fmt);}/* * getkval(offset, ptr, size, refstr) - get a value out of the kernel. * "offset" is the byte offset into the kernel for the desired value, * "ptr" points to a buffer into which the value is retrieved, * "size" is the size of the buffer (and the object to retrieve), * "refstr" is a reference string used when printing error meessages, * if "refstr" starts with a '!', then a failure on read will not * be fatal (this may seem like a silly way to do things, but I * really didn't want the overhead of another argument). * */int getkval(offset, ptr, size, refstr) off_t offset; int *ptr; int size; char *refstr;{ if (lseek(kmem, offset, SEEK_SET) == -1) { if (*refstr == '!') refstr++; (void) fprintf(stderr, "%s: lseek to %s: %s\n", KMEM, refstr, strerror(errno)); quit(0); } if (read(kmem, (char *) ptr, size) == -1) { if (*refstr == '!') return(0); else { (void) fprintf(stderr, "%s: reading %s: %s\n", KMEM, refstr, strerror(errno)); quit(0); } } return(1);}/* * proc_compare - comparison function for "qsort" * Compares the resource consumption of two processes using five * distinct keys. The keys (in descending order of importance) are: * percent cpu, cpu ticks, state, resident set size, total virtual * memory usage. The process states are ordered as follows (from least * to most important): WAIT, zombie, sleep, stop, idle, run. The * array declaration below maps a process state index into a number * that reflects this ordering. */unsigned char sorted_state[] ={ 0, /* not used */ 3, /* sleep */ 6, /* run */ 2, /* zombie */ 4, /* stop */ 5, /* idle */ 0, /* not used */ 1 /* being swapped (WAIT) */};int proc_compare (pp1, pp2) struct prpsinfo **pp1; struct prpsinfo **pp2;{ register struct prpsinfo *p1; register struct prpsinfo *p2; register long result; /* remove one level of indirection */ p1 = *pp1; p2 = *pp2; /* compare percent cpu (pctcpu) */ if ((result = (long) (p2->pr_cpu - p1->pr_cpu)) == 0) { /* use cpticks to break the tie */ if ((result = p2->pr_time.tv_sec - p1->pr_time.tv_sec) == 0) { /* use process state to break the tie */ if ((result = (long) (sorted_state[p2->pr_state] - sorted_state[p1->pr_state])) == 0) { /* use priority to break the tie */ if ((result = p2->pr_oldpri - p1->pr_oldpri) == 0) { /* use resident set size (rssize) to break the tie */ if ((result = p2->pr_rssize - p1->pr_rssize) == 0) { /* use total memory to break the tie */ result = (p2->pr_size - p1->pr_size); } } } } } return (result);}/* return the owner of the specified process. */uid_t proc_owner (pid) pid_t pid;{ register struct prpsinfo *p; int i; for (i = 0, p = pbase; i < nproc; i++, p++) if (p->pr_pid == pid) return (p->pr_uid); return (-1);}/* * check_nlist(nlst) - checks the nlist to see if any symbols were not * found. For every symbol that was not found, a one-line * message is printed to stderr. The routine returns the * number of symbols NOT found. */int check_nlist(nlst) register struct nlist *nlst;{ register int i; /* check to see if we got ALL the symbols we requested */ /* this will write one line to stderr for every symbol not found */ i = 0; while (nlst->n_name != NULL) { if (nlst->n_type == 0) { /* this one wasn't found */ fprintf(stderr, "kernel: no symbol named `%s'\n", nlst->n_name); i = 1; } nlst++; } return(i);}/* get process table */void getptable (baseptr) struct prpsinfo *baseptr;{ struct prpsinfo *currproc; /* pointer to current proc structure */ struct prcred currcred; int numprocs = 0; int i; struct dirent *directp; struct oldproc *op; static struct timeval lasttime = {0L, 0L}; struct timeval thistime; struct timezone thiszone; double timediff; double alpha, beta; struct oldproc *endbase; gettimeofday (&thistime, &thiszone); /* * To avoid divides, we keep times in nanoseconds. This is * scaled by 1e7 rather than 1e9 so that when we divide we * get percent. */ if (lasttime.tv_sec) timediff = ((double) thistime.tv_sec * 1.0e7 + ((double) thistime.tv_usec * 10.0)) - ((double) lasttime.tv_sec * 1.0e7 + ((double) lasttime.tv_usec * 10.0)); else timediff = 1.0e7; /* * constants for exponential average. avg = alpha * new + beta * avg * The goal is 50% decay in 30 sec. However if the sample period * is greater than 30 sec, there's not a lot we can do. */ if (timediff < 30.0e7) { alpha = 0.5 * (timediff / 30.0e7); beta = 1.0 - alpha; } else { alpha = 0.5; beta = 0.5; } endbase = oldbase + oldprocs; currproc = baseptr; for (rewinddir (procdir); directp = readdir (procdir);) { int fd; /* * ignore names with leading dots: both ioctls below work * for spurious (thread related?) entries */ if (directp->d_name[0] == '.') continue; if ((fd = open (directp->d_name, O_RDONLY)) < 0) continue; currproc = &baseptr[numprocs]; if (ioctl (fd, PIOCPSINFO, currproc) < 0 || /* ps(1) uses this to check for dummy procfs entries */ ioctl(fd, PIOCCRED, &currcred) < 0) { (void) close (fd); continue; } /* * SVr4 doesn't keep track of CPU% in the kernel, so we have * to do our own. See if we've heard of this process before. * If so, compute % based on CPU since last time. */ op = oldbase + HASH (currproc->pr_pid); while (1) { if (op->oldpid == -1) /* not there */ break; if (op->oldpid == currproc->pr_pid) { /* found old data */ percent_cpu (currproc) = ((currproc->pr_time.tv_sec * 1.0e9 + currproc->pr_time.tv_nsec) - op->oldtime) / timediff; weighted_cpu (currproc) = op->oldpct * beta + percent_cpu (currproc) * alpha; break; } op++; /* try next entry in hash table */ if (op == endbase) /* table wrapped around */ op = oldbase; } /* Otherwise, it's new, so use all of its CPU time */ if (op->oldpid == -1) { if (lasttime.tv_sec) { percent_cpu (currproc) = (currproc->pr_time.tv_sec * 1.0e9 + currproc->pr_time.tv_nsec) / timediff; weighted_cpu (currproc) = percent_cpu (currproc); } else { /* first screen -- no difference is possible */ percent_cpu (currproc) = 0.0; weighted_cpu (currproc) = 0.0; } } numprocs++; (void) close (fd); } if (nproc != numprocs) nproc = numprocs; /* * Save current CPU time for next time around * For the moment recreate the hash table each time, as the code * is easier that way. */ oldprocs = 2 * nproc; endbase = oldbase + oldprocs; for (op = oldbase; op < endbase; op++) op->oldpid = -1; for (i = 0, currproc = baseptr; i < nproc; i++, currproc = (struct prpsinfo *) ((char *) currproc + PRPSINFOSIZE)) { /* find an empty spot */ op = oldbase + HASH (currproc->pr_pid); while (1) { if (op->oldpid == -1) break; op++; if (op == endbase) op = oldbase; } op->oldpid = currproc->pr_pid; op->oldtime = (currproc->pr_time.tv_sec * 1.0e9 + currproc->pr_time.tv_nsec); op->oldpct = weighted_cpu (currproc); } lasttime = thistime;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -