我有一个在 ARM
上运行的 Embedded Linux(Kernel 2.6.37)
。 busybox 1.13.2
有一个默认的top
。我通过交叉编译构建了一个 procps-ng 3.3.11
以在这个 Linux
上运行。我发现 busybox
和 procps-ng
通过 top
输出的进程的 %cpu
是不同的。
比如一个进程的%cpu
,procps-ng top
显示30%左右,而busybox top
只显示10左右%。 procps-ng top
和 busybox top
的总 %cpu
相同。
然后看了busybox
和procps-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/