sort by %cpu redone

This commit is contained in:
albert 2002-10-09 23:40:30 +00:00
parent 64808793eb
commit 667b1bc2fa
2 changed files with 12 additions and 10 deletions

View File

@ -207,16 +207,19 @@ static void check_needs(void){
} }
/***** fill in %CPU; not in libproc because of include_dead_children */ /***** fill in %CPU; not in libproc because of include_dead_children */
/* Note: for sorting, not display, so 0..0x7fffffff would be OK */
static void fill_pcpu(proc_t *buf){ static void fill_pcpu(proc_t *buf){
unsigned long total_time; unsigned long long used_jiffies;
unsigned long pcpu = 0; unsigned long pcpu = 0;
unsigned long seconds; unsigned long long avail_jiffies;
total_time = buf->utime + buf->stime; used_jiffies = buf->utime + buf->stime;
if(include_dead_children) total_time += (buf->cutime + buf->cstime); if(include_dead_children) used_jiffies += (buf->cutime + buf->cstime);
seconds = seconds_since_boot - buf->start_time / Hertz;
if(seconds) pcpu = (total_time * 1000ULL / Hertz) / seconds; avail_jiffies = seconds_since_boot * Hertz - buf->start_time;
buf->pcpu = (pcpu > 999) ? 999 : pcpu; if(avail_jiffies) pcpu = (used_jiffies << 24) / avail_jiffies;
buf->pcpu = pcpu; // fits in an int, summing children on 128 CPUs
} }
/***** figure out what we need */ /***** figure out what we need */
@ -243,7 +246,6 @@ static void simple_spew(void){
memset(&buf, '#', sizeof(proc_t)); memset(&buf, '#', sizeof(proc_t));
/* use "ps_" prefix to catch library mismatch */ /* use "ps_" prefix to catch library mismatch */
while(ps_readproc(ptp,&buf)){ while(ps_readproc(ptp,&buf)){
fill_pcpu(&buf);
if(want_this_proc(&buf)) show_one_proc(&buf); if(want_this_proc(&buf)) show_one_proc(&buf);
if(buf.cmdline) free((void*)*buf.cmdline); // ought to reuse if(buf.cmdline) free((void*)*buf.cmdline); // ought to reuse
if(buf.environ) free((void*)*buf.environ); // ought to reuse if(buf.environ) free((void*)*buf.environ); // ought to reuse
@ -380,8 +382,8 @@ static void fancy_spew(void){
exit(1); exit(1);
} }
while((retbuf = ps_readproc(ptp,retbuf))){ while((retbuf = ps_readproc(ptp,retbuf))){
fill_pcpu(retbuf);
if(want_this_proc(retbuf)){ if(want_this_proc(retbuf)){
fill_pcpu(retbuf); // in case we might sort by %cpu
processes[n++] = retbuf; processes[n++] = retbuf;
retbuf = NULL; /* NULL asks ps_readproc to allocate */ retbuf = NULL; /* NULL asks ps_readproc to allocate */
} }

View File

@ -428,7 +428,7 @@ static int pr_nice(void){
/* "Processor utilisation for scheduling." --- we use %cpu w/o fraction */ /* "Processor utilisation for scheduling." --- we use %cpu w/o fraction */
static int pr_c(void){ static int pr_c(void){
unsigned long long total_time; /* jiffies used by this process */ unsigned long long total_time; /* jiffies used by this process */
unsigned pcpu = 0; /* scaled %cpu, 999 means 99.9% */ unsigned pcpu = 0; /* scaled %cpu, 99 means 99% */
unsigned long long seconds; /* seconds of process life */ unsigned long long seconds; /* seconds of process life */
total_time = pp->utime + pp->stime; total_time = pp->utime + pp->stime;
if(include_dead_children) total_time += (pp->cutime + pp->cstime); if(include_dead_children) total_time += (pp->cutime + pp->cstime);