Linux上的平均负载( /proc/loadavg
,也由uptime
和top
等报告)可以衡量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)的作业)的负载平均指标。有谁知道我是否可以得到这个?
简短的回答是“不,你不能。”
以下具体内容涉及内核版本3.8。 一些变量和函数定义的位置随着时间的推移而变化,所以这不适用于最新的内核和3.4以前的内核。
计算的加载平均值存储在struct taskinfo
( source )的三槽数组中。 负载平均值也会在kernel/sched/core.c
定义的另一个三槽阵列avenrun[3]
中kernel/sched/core.c
(参见本文 )。 不计算等待I / O的线程的负载平均值不会被计算出来,所以你必须kernel/sched/core.c
在kernel/sched/core.c
如何计算事物(例如参见函数calc_load_n()
和calc_global_nohz
) 。
/ proc / loadavg通过跟踪{正在运行的任务的数量+正在等待的任务的数量}来计算,采样率是5秒。
所以,我认为这使它成为可能:
如果只想跟踪正在运行的任务的数量,则可以制作一个简单的应用程序,定期读取/ proc / stat并在procs_running上打印出指数移动平均值。