ps: fix cpu utilization sorting

The sniplet below resulted not to be sorted correctly.

for i in $(seq 1 50); do ls -R /usr &>/dev/null 2>&1 & done
    sleep 1; ps -e --sort=pcpu -o pcpu,comm=; pkill ls

Issue is present since older versions of procps (3.2.7/3.2.8).

Reference: http://www.freelists.org/post/procps/top-incorrect-sort,2
Reported-by: Jaromir Capik <jcapik@redhat.com>
Backported-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
Jan Grig 2012-04-23 20:45:10 +02:00 committed by Craig Small
parent 43d4aac43f
commit cdeb9a071a

View File

@ -314,15 +314,15 @@ static void lists_and_needs(void){
static int want_this_proc_pcpu(proc_t *buf){ static int want_this_proc_pcpu(proc_t *buf){
unsigned long long used_jiffies; unsigned long long used_jiffies;
unsigned long pcpu = 0; unsigned long pcpu = 0;
unsigned long long avail_jiffies; unsigned long long seconds;
if(!want_this_proc(buf)) return 0; if(!want_this_proc(buf)) return 0;
used_jiffies = buf->utime + buf->stime; used_jiffies = buf->utime + buf->stime;
if(include_dead_children) used_jiffies += (buf->cutime + buf->cstime); if(include_dead_children) used_jiffies += (buf->cutime + buf->cstime);
avail_jiffies = seconds_since_boot * Hertz - buf->start_time; seconds = seconds_since_boot - buf->start_time / Hertz;
if(avail_jiffies) pcpu = (used_jiffies << 24) / avail_jiffies; if(seconds) pcpu = (used_jiffies * 1000ULL / Hertz) / seconds;
buf->pcpu = pcpu; // fits in an int, summing children on 128 CPUs buf->pcpu = pcpu; // fits in an int, summing children on 128 CPUs