]> granicus.if.org Git - procps-ng/commitdiff
ps: fix cpu utilization sorting
authorJan Görig <jgorig@redhat.com>
Mon, 23 Apr 2012 18:45:10 +0000 (20:45 +0200)
committerCraig Small <csmall@enc.com.au>
Tue, 24 Apr 2012 23:34:06 +0000 (09:34 +1000)
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>
ps/display.c

index a1a0f5bb1192b8b86084de63d5509aba377e62d0..cd0fb3282a593521b62fbff2ee713df8bd319fe7 100644 (file)
@@ -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