linux - 不同进程的 %cpu 通过 busybox 和 procps-ng 的 top 输出

标签 linux linux-kernel embedded-linux busybox procps

我有一个在 ARM 上运行的 Embedded Linux(Kernel 2.6.37)busybox 1.13.2 有一个默认的top。我通过交叉编译构建了一个 procps-ng 3.3.11 以在这个 Linux 上运行。我发现 busyboxprocps-ng 通过 top 输出的进程的 %cpu 是不同的。

比如一个进程的%cpuprocps-ng top显示30%左右,而busybox top只显示10左右%。 procps-ng topbusybox top 的总 %cpu 相同。
然后看了busyboxprocps-ng的计算源码。我发现他们对一个进程的%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  

为什么两个项目选择不同的计算公式?他们是为不同的应用案例设计的吗? 谢谢!

最佳答案

已经和procps-ng团队讨论过这个问题,这里是重点:

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.

更多详情请引用讨论链接:
discussion: different process's %cpu output via top from busybox and procps-ng

关于linux - 不同进程的 %cpu 通过 busybox 和 procps-ng 的 top 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41134205/

相关文章:

linux - 在 Yocto ext4 Image 中设置 Linux 功能

java - buildroot 与 jamvm 2.0 for java 8

c++ - 使用 ioctl 修改 IP 地址时出现不需要的副作用

linux - 如何在击键中断后初始化 tasklet?

linux - 如何知道 schedule() 调用何时因信号返回?

c - 制作一个/dev/hwrng 内核模块,它使用光标位置来生成熵

c - 软件和硬件中断的区别

python - 如何使用 Paramiko 通过中间主机 SSH 连接到机器?

linux - 如何添加详细选项来控制脚本输出?

linux - 以root身份在/etc/profile.d/at Startup中执行脚本?