What is the difference in CPU utilization between 'ps aux' and 'ps -ef'?

16,425

Solution 1

The %cpu and C columns are showing almost, but not quite, the same thing. If you look at the source for ps in ps/output.c you can see the differences between pr_c and pr_cpu

C is the integer value for %cpu as you can guess. The odd difference is that C is clamped to a maximum of 99 while %cpu is not (there's a check for it for %cpu but it just changes the format from xx.x% to xxx%).

Now, I'm not really sure why C has this clamping; it seems a little arbitrary. It's been there since procps 3.2.7 (2006) so it probably was from the era of single CPUs

Solution 2

Note: I am not using the same Linux as you, so this is more of a suggestion for testing that an explicit answer. I hope it still helps and I would be really interested in seeing if you get the same results.

From the tests I have ran the values are the same apart from truncation (it doesn't round) of the CPU percentage values for the C column.

I suggest running the following test to see if you get similar value. Start a process that will use some resources:

dd if=/dev/zero of=/dev/null & 
DD_PID=$!

Then perform the check of process using ; to make each call as close to simultaneous as possible:

ps -f --pid $DD_PID | head ; echo --- ; ps -u -p $DD_PID | head

It should generate something like:

UID        PID  PPID  C STIME TTY          TIME CMD
someuser  2743 21793 99 10:38 pts/25   00:24:37 dd if=/dev/zero of=/dev/null
---
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
someuser  2743 99.9  0.0 107940   656 pts/25   R    10:38  24:37 dd if=/dev/zero of=/dev/null

Note: I have used -f instead of -ef, and -u instead of aux however both are meant to change the columns and filtering used, not the content. If desired you could switch to using a grep and drop the -p PID.

Share:
16,425

Related videos on Youtube

monkeyhouse
Author by

monkeyhouse

Updated on September 18, 2022

Comments

  • monkeyhouse
    monkeyhouse over 1 year

    I use Ubuntu 12.04.1 Linux. I see a difference between %CPU and C output format of ps command for a process. It is not clearly noted in the ps man page.

    Man pages says:

      CODE       HEADER  DESCRIPTION
      %cpu       %CPU    cpu utilization of the process in "##.#" format. Currently, 
                         it is the CPU time used divided by the time the 
                         process has been running (cputime/realtime ratio),
                         expressed as a percentage. It will not add up to 100%
                         unless you are lucky. (alias pcpu).
      c          C       processor utilization. Currently, this is the integer                  
                         value of the percent usage over the lifetime of the
                         process. (see %cpu).
    

    So basically it should be the same, but it is not:

    $ ps aux | head -1
    USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    $ ps aux | grep 32473
    user     32473  151 38.4 18338028 6305416 ?    Sl   Feb21 28289:48 ZServer -server -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./log 
    
    
    $ ps -ef | head -1
    UID        PID  PPID  C STIME TTY          TIME CMD
    $ ps -ef | grep 32473
    user     32473 32472 99 Feb21 ?        19-15:29:50 ZServer -server -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=./log 
    

    The top shows 2% CPU utilization in the same time. I know the 'top' shows the current CPU utilization while ps shows CPU utilization over the lifetime of the process.

    I guess the lifetime definition is somewhat different for these two format options.

    • Admin
      Admin about 10 years
      Maybe 99 is the maximum value that ps -ef could display ?
    • Admin
      Admin about 10 years
      You can check out this - unix.stackexchange.com/a/28724/55815 I think that answers your question
    • Admin
      Admin about 10 years
      @ganesh737 Thanks for the suggestion but this does not actually answers my question - what is the difference. The answer says a lot about CPU utilization over the lifetime of the process. This is what I already know.