确定 CPU 利用率
是否有命令或任何其他方法可以获取 Linux 中当前或平均 CPU 利用率(对于多处理器环境)?
我在一个小型系统中使用嵌入式Linux。基本上,我需要确定 CPU 利用率,以便如果它很高,我可以将新进程转移到系统中的另一个控制器,而不是在主处理器上执行,因为主处理器可能正忙于执行更重要的进程。
这个问题不仅仅是关于进程的优先级,另一个控制器可以充分处理新进程,只是当主处理器不忙时,我更喜欢它来执行。
Is there a command or any other way to get the current or average CPU utilization (for a multi-processor environment) in Linux?
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
经过大量搜索和修改后,问题的答案是:
我得到的值与系统监视器报告的值相同。
The answer to the question after much searching and tinkering:
I am getting the same values as those reported by the System Monitor.
您将看到类似这样的内容
只需将前三个数字的总和除以前四个整数的总和
前 4 个数字是用户、nice、系统和空闲时间
注意:这给出了总体平均值。如果你想取自发平均值,你应该取两个样本并在除法之前将它们相减。
you will see something like this
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
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.
您需要对
/proc/stat
中的值进行两次采样,并计算该时间段内的平均利用率。 (瞬时利用率没有多大意义——在单核机器上它总是 100%,因为你的利用率测量代码无论何时看起来都在运行)。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 文件系统有各种有趣的信息。请参阅
man proc
了解更多信息。The /proc filesystem has all kinds of interesting information. Look at
man proc
for more information.如果可用,只需使用 top 即可。您可以在非交互模式下使用它:
如果您想要特定的内容,那么只需 grep 该输出即可。确切的细节取决于您的 top 命令如何格式化其输出,例如:
Just use top if it is available. You can use it in a non-interactive mode:
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: