From: Jan Görig Date: Mon, 23 Apr 2012 18:45:10 +0000 (+0200) Subject: ps: fix cpu utilization sorting X-Git-Tag: v3.3.3~16 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cdeb9a071ad2ae56f1395bca75845bfff7c76694;p=procps-ng 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 Backported-by: Sami Kerola --- diff --git a/ps/display.c b/ps/display.c index a1a0f5bb..cd0fb328 100644 --- a/ps/display.c +++ b/ps/display.c @@ -314,15 +314,15 @@ static void lists_and_needs(void){ static int want_this_proc_pcpu(proc_t *buf){ unsigned long long used_jiffies; unsigned long pcpu = 0; - unsigned long long avail_jiffies; + unsigned long long seconds; if(!want_this_proc(buf)) return 0; used_jiffies = buf->utime + buf->stime; if(include_dead_children) used_jiffies += (buf->cutime + buf->cstime); - avail_jiffies = seconds_since_boot * Hertz - buf->start_time; - if(avail_jiffies) pcpu = (used_jiffies << 24) / avail_jiffies; + seconds = seconds_since_boot - buf->start_time / Hertz; + if(seconds) pcpu = (used_jiffies * 1000ULL / Hertz) / seconds; buf->pcpu = pcpu; // fits in an int, summing children on 128 CPUs