测量Linux上的CPU负载平均值(不包括磁盘负载)

时间:2014-07-22 10:34:01

标签: linux linux-kernel load

Linux上的负载平均值(/proc/loadavg,也由uptimetop报告等)衡量CPU和磁盘负载:

来自man 5 proc

   /proc/loadavg
          The first three fields in this file  are  load  average  figures
          giving  the number of jobs in the run queue (state R) or waiting
          for disk I/O (state D) averaged over 1, 5, and 15 minutes.  They
          are  the same as the load average numbers given by uptime(1) and
          other programs.  The fourth field consists of two numbers  sepa-
          rated  by a slash (/).  The first of these is the number of cur-
          rently  executing   kernel   scheduling   entities   (processes,
          threads); this will be less than or equal to the number of CPUs.
          The value after the slash is the  number  of  kernel  scheduling
          entities that currently exist on the system.  The fifth field is
          the PID of the process that was most  recently  created  on  the
          system.

我真的很想找到只是CPU负载的加载平均指标(运行队列中的作业数量(状态R),排除等待的作业对于磁盘I / O(状态D)。有谁知道我能不能得到它?

2 个答案:

答案 0 :(得分:2)

简短的回答是“不,你不能。”

以下细节涉及内核版本3.8。某些变量和函数定义的位置随着时间的推移而发生了变化,因此这不适用于最新的内核以及早于3.4的内核。

计算的平均负载存储在struct taskinfosource)的三个插槽阵列中。在avenrun[3]中定义的另外三个插槽阵列kernel/sched/core.c中也会加载平均值(请参阅this)。不计算等待I / O的线程的负载平均值不会被计算,因此您必须在kernel/sched/core.c中计算事物的计算方式(例如,参见函数calc_load_n()和{{1 }} here)。

答案 1 :(得分:2)

/ proc / loadavg的计算方法是跟踪{正在运行的任务的数量+等待任务的数量},采样率为5秒。

所以,我认为这使它成为可能:

如果你只想跟踪正在运行的任务的数量,你可以创建一个简单的应用程序,定期读取/ proc / stat并在procs_running上打印出指数移动平均值。