bash 从 /proc/stat 计算 CPU 使用率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15013238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:37:51  来源:igfitidea点击:

Calculating CPU usage from /proc/stat

bash

提问by hexacyanide

When reading /proc/stat, I get these return values:

阅读时/proc/stat,我得到这些返回值:

cpu  20582190 643 1606363 658948861 509691 24 112555 0 0 0                      
cpu0 3408982 106 264219 81480207 19354 0 35 0 0 0                               
cpu1 3395441 116 265930 81509149 11129 0 30 0 0 0                               
cpu2 3411003 197 214515 81133228 418090 0 1911 0 0 0                            
cpu3 3478358 168 257604 81417703 30421 0 29 0 0 0                               
cpu4 1840706 20 155376 83328751 1564 0 7 0 0 0                                  
cpu5 1416488 15 171101 83410586 1645 13 108729 0 0 0                            
cpu6 1773002 7 133686 83346305 25666 10 1803 0 0 0                              
cpu7 1858207 10 143928 83322929 1819 0 8 0 0 0

Some sources state to read only the first four values to calculate CPU usage, while some sources say to read all the values.

一些消息来源声明只读取前四个值来计算 CPU 使用率,而一些消息来源说读取所有值。

Do I read only the first four values to calculate CPU utilization; the values user, nice, system, and idle? Or do I need all the values? Or not all, but more than four? Would I need iowait, irq, or softirq?

我是否只读取前四个值来计算 CPU 利用率?值user, nice, system, 和idle? 还是我需要所有的值?或者不是全部,而是超过四个?我需要iowait, irq, 或softirq吗?

cpu  20582190 643 1606363

Versus the entire line.

与整条线相对。

cpu  20582190 643 1606363 658948861 509691 24 112555 0 0 0

Edits:Some sources also state that iowaitis added into idle.

编辑:一些来源还指出iowait已添加到idle.

When calculating a specific process' CPU usage, does the method differ?

计算特定进程的CPU使用率时,计算方法有区别吗?

采纳答案by bsravanin

The man pagestates that it varies with architecture, and also gives a couple of examples describing how they are different:

手册页指出,它与架构变化,并给出了几个描述他们是如何不同的例子:

In Linux 2.6 this line includes three additional columns: ...

Since Linux 2.6.11, there is an eighth column, ...

Since Linux 2.6.24, there is a ninth column, ...

在 Linux 2.6 中,这一行包括三个额外的列:...

从 Linux 2.6.11 开始,有第八列,...

从 Linux 2.6.24 开始,有第九列,...

When "some people said to only use..." they were probably not taking these into account.

当“有些人说只使用……”时,他们可能没有考虑到这些。

Regarding whether the calculation differs across CPUs: You will find lines related to "cpu", "cpu0", "cpu1", ... in /proc/stat. The "cpu" fields are all aggregates (not averages) of corresponding fields for the individual CPUs. You can check that for yourself with a simple awk one-liner.

关于计算是否因 CPU 不同而不同:您将在 /proc/stat 中找到与“cpu”、“cpu0”、“cpu1”、...相关的行。“cpu”字段是各个 CPU 的相应字段的所有聚合(不是平均值)。您可以使用简单的 awk one-liner 自行检查。

cpu 84282 747 20805 1615949 44349 0 308 0 0 0

cpu0 26754 343 9611 375347 27092 0 301 0 0 0

cpu1 12707 56 2581 422198 5036 0 1 0 0 0

cpu2 33356 173 6160 394561 7508 0 4 0 0 0

cpu3 11464 174 2452 423841 4712 0 1 0 0 0

CPU 84282 747 20805 1615949 44349 0 308 0 0 0

cpu0 26754 343 9611 375347 27092 0 301 0 0 0

CPU1 12707 56 2581 422198 5036 0 1 0 0 0

cpu2 33356 173 6160 394561 7508 0 4 0 0 0

cpu3 11464 174 2452 423841 4712 0 1 0 0 0