Linux 确定 CPU 利用率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3769405/
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
Determining CPU utilization
提问by
Is there a command or any other way to get the current or average CPU utilization(for a multi-processor environment) in Linux?
是否有命令或任何其他方式来获取Linux 中的当前或平均CPU 利用率(对于多处理器环境)?
I am using embedded Linux in a small system. Basically, I need to determine the CPU utilization, so that if it is high, I can instead divert a new process to another controller in the system, rather than executing on the main processor, which could be busy doing a more important process.
我在一个小型系统中使用嵌入式 Linux。基本上,我需要确定 CPU 利用率,以便如果它很高,我可以将新进程转移到系统中的另一个控制器,而不是在主处理器上执行,主处理器可能忙于执行更重要的进程。
This question is not about merely prioritizing processes, the other controller can sufficiently handle the new process, just that when the main processor is not busy, I would prefer it to do the execution.
这个问题不仅仅是优先处理进程,另一个控制器可以充分处理新进程,只是当主处理器不忙时,我更愿意它来执行。
采纳答案by caf
You need to sample the values in /proc/stat
at two times, and calculate the average utilisation over that time. (Instantaneous utilisation doesn't make a whole lot of sense - it'll always be 100% on a single core machine, since your utilsation-measuring code is running whenever it looks).
您需要对值/proc/stat
进行两次采样,并计算这段时间内的平均利用率。(即时利用率并没有多大意义——它在单核机器上总是 100%,因为您的利用率测量代码无论何时都在运行)。
回答by doron
The /proc filesystem has all kinds of interesting information. Look at man proc
for more information.
/proc 文件系统有各种有趣的信息。查看man proc
更多信息。
回答by Andrew
Just use top if it is available. You can use it in a non-interactive mode:
如果可用,只需使用 top 。您可以在非交互模式下使用它:
top -n 1
If you want something specific then just grep that output. The exact details will depend on how your top command formats its output, but for example:
如果你想要一些特定的东西,那么只需 grep 那个输出。确切的细节将取决于您的 top 命令如何格式化其输出,但例如:
top -n 1 | grep 'Load'
回答by oguzalb
cat /proc/stat
you will see something like this
你会看到这样的东西
cpu 178877 11039 58012 5027374 22025 2616 1298 0 0
cpu0 122532 8808 34213 2438147 10881 1050 448 0 0
cpu1 56344 2230 23799 2589227 11143 1565 850 0 0
Simply take the sums of first three numbers and divide them with sums of first four integer
只需取前三个数字的总和并将它们除以前四个整数的总和
The first 4 numbers are user, nice, system, and idle times
前 4 个数字是用户、nice、系统和空闲时间
note: This gives overall average. If you want to take spontaneous average, you should take two samples and subtract them from each other before the divide.
注意:这给出了总体平均值。如果要取自发平均值,则应取两个样本并在除法之前将它们相减。
回答by oguzalb
The answer to the question after much searching and tinkering:
经过大量搜索和修补后问题的答案:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long double a[4], b[4], loadavg;
FILE *fp;
char dump[50];
for(;;)
{
fp = fopen("/proc/stat","r");
fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3]);
fclose(fp);
sleep(1);
fp = fopen("/proc/stat","r");
fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3]);
fclose(fp);
loadavg = ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
printf("The current CPU utilization is : %Lf\n",loadavg);
}
return(0);
}
I am getting the same values as those reported by the System Monitor.
我得到的值与系统监视器报告的值相同。