过去 X 分钟的 Linux CPU 负载

标签 linux

专家您好!
能否请你帮忙。我想计算过去 5 分钟的 CPU 负载。我需要永久获取这些值。

我找到了非常有用和详细的文章:http://phoxis.org/2013/09/05/finding-overall-and-per-core-cpu-utilization/
但我不确定检索该数字的最佳方法是什么:不断每秒运行一次脚本,并每 300 次迭代计算一次超额数量(例如)? 提前致谢!

最佳答案

如果您编写自己的代码来监控使用情况,您将面临相当于海森堡不确定性原理的计算。或者,您可以使用“正常运行时间”命令。

% uptime
  16:00:29 up 3 days, 18:31,  2 users,  load average: 0.04, 0.12, 0.09

最后三个数字是过去 1,5 和 15 分钟的系统平均负载。从手册页:

   System load averages is the average number of processes that are
   either in a runnable or uninterruptable state.  A process  in  a
   runnable  state  is  either  using the CPU or waiting to use the
   CPU.  A process in uninterruptable state is waiting for some I/O
   access,  eg  waiting  for disk.  The averages are taken over the
   three time intervals.  Load averages are not normalized for  the
   number  of CPUs in a system, so a load average of 1 means a sin-
   gle CPU system is loaded all the time while on a 4 CPU system it
   means it was idle 75% of the time.

只要您知道系统中有多少个 CPU,这就能满足您的需求。

第二次迭代:

# cat /proc/uptime
353615.60 344666.42

来自/proc 手册页:

   /proc/uptime
          This file contains two numbers: the uptime of the sys-
          tem  (seconds),  and  the amount of time spent in idle
          process (seconds).

您可以每五分钟轮询一次并记录值的变化。

关于过去 X 分钟的 Linux CPU 负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29370939/

相关文章:

linux - 终止正在 Q​​Thread 中运行的正在进行的 QProcess?

mysql - 对于空值,SQlite select 返回 "(null)"而不是 ""

linux - 从内核中断读取设备

linux - 需要有关使用 sed 格式化行的帮助

linux - 无法在主目录 : permission denied (Linux Lubuntu) 中使用 mkdir

linux - 如何使用 docker import 导入多个图像?

linux - 如何在 Linux 中使用 SED 正则表达式从一行中提取多个字符串并将它们写入文件?

linux - 尝试做关于递归的大学练习

linux - 顶级批处理模式下的 CPU 使用率

java - 在 Ubuntu 上运行 JavaFX .jar 文件时出错