different process's %cpu output via top from busybox and procps-ng

1.4k views Asked by At

I have an Embedded Linux(Kernel 2.6.37) running on ARM. There's a default top from busybox 1.13.2. And I build a procps-ng 3.3.11 via cross compile to run on this Linux. I found that the process's %cpu via top output from busybox and procps-ng are different.

For example, the %cpu of one process, procps-ng top display about 30%, but busybox top display only about 10%. The total %cpu from procps-ng top and busybox top are the same.
Then I read the calculation source code of busybox and procps-ng. I found that they really have different calculation formula for one process's %cpu.


- busybox top:  
    CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks   
    (pcpu is delta of sys+user time between samples)  

- procps-ng top: 
    CPU% = s->pcpu/total_cpu_ticks  

Why the two projects choose different calculation formula? Do they designed for different application case? Thanks!

1

There are 1 answers

0
Jay Zhang On

Have discussed this issue with the procps-ng team, and here's the key point:

On 12/28/2016 03:12 PM, Craig Small wrote:
There is probably a different ways to look at this. The first thing I would say is that busybox is supposed to be emulating or mimicking what the main programs are supposed to do, so if there is a difference between busybox and the real program, I would say that busybox is wrong. It would be like the busybox ls printing files differently to the real ls.

Let's look at this formula again:

  • busybox top:
    CPU% = s->pcpu/sum(s->pcpu) * busy_cpu_ticks/total_cpu_ticks
    (pcpu is delta of sys+user time between samples)

  • procps-ng top:
    CPU% = s->pcpu/total_cpu_ticks

Now let's re-arrange it:

  • busybox top:
    CPU% = s->pcpu/total_cpu_ticks * busy_cpu_ticks/sum(s->pcpu)
    CPU% = top_CPU% * busy_cpu_ticks/sum(s->pcpu)
    (pcpu is delta of sys+user time between samples)

This is the difference. busybox adds a ratio of busy_cpu_ticks/sum(s->pcpu) to what top worked out. This ratio could be written as:

RATIO = Sum(Usr + Nice + System + Irq + sirq + steal) / Sum(Usr + System)

You can sort of see this in read_cpu_jiffy in the busybox source. I don't understand what this ratio is trying to do or why it is required. Especially when the program you are trying to emulate doesn't have it.

procps top says out of the total available jiffies in this cycle, X% were used by this program. busybox puts some odd scaling in there. So if a process uses 10% of the CPU in its system and user parts, top shows 10%. If for that cycle The process is in User and System for 50% of the time, busybox would show 20% (10% x 100/50).

I don't know why they did that.

Please refer the discussion link for more details:
discussion: different process's %cpu output via top from busybox and procps-ng