在Linux上测量CPU平均负载(排除磁盘负载)

5

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个回答

3

简短的回答是“不行”。

以下特定内容仅适用于内核版本3.8。随着时间的推移,一些变量和函数定义的位置已经发生了变化,因此这并不适用于最新的内核或早于3.4的内核。

计算的平均负载存储在一个三插槽数组的结构体taskinfo中(源代码)。平均负载也在另一个三插槽数组avenrun[3]中传递,该数组在kernel/sched/core.c中定义(请参见此处)。除等待I/O的线程外的平均负载值根本没有被计算,因此您需要修改kernel/sched/core.c中的计算方式(例如,这里calc_load_n()calc_global_nohz函数)。


3

/proc/loadavg 是通过跟踪{正在运行的任务数+等待io的任务数}计算得出的,采样率为5秒。

因此,我认为这是可能的:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接