From cdeb9a071ad2ae56f1395bca75845bfff7c76694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20G=F6rig?= Date: Mon, 23 Apr 2012 20:45:10 +0200 Subject: [PATCH] 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 --- ps/display.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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