共计 2499 个字符,预计需要花费 7 分钟才能阅读完成。
average load? 表示系统在一段时间内的平均进程个数,也就是表示系统的繁忙程度。average load 和 CPU 利用率不一样,更加能够表示系统的繁忙程度,下面将就系统的 average load 的计算和相关进行简单介绍。
查看方法
在 Linux 系统下使用 uptime 命令,或者查看 /proc/loadavg 都可以看到系统负载 average load。使用 uptime 命令会显示系统分别在过去的 1 分钟,5 分钟和 10 分钟里的平均负载。
[root@localhost ~]# uptime
19:32:09 up 5 days, 8:53, 5 users, load average: 0.05, 0.04, 0.05
[root@localhost ~]# cat /proc/loadavg
0.04 0.04 0.05 1/394 23203
那么 uptime 命令计算 load average 的工作原理是什么呢?
计算方法
对于单 cpu 和多 cpu 情况,系统的 average load 情况稍有不同。单 cpu 是最简单的情形,比如过去的平均一分钟里面,判断系统处于运行或者等待状态的进程数则表示系统的平均负载,但是在 linux 系统下稍有不同,那些处于 io 等待状态的进程也会被纳入去计算。这样就导致 CPU 利用率可能和平均负载很不同,在大部分进程都在做 IO 的时候,即使平均负载很大,也会没有很大的 CPU 利用率。另外,有些系统对于进程和线程的处理也很不一样,有的对于每一个线程都会进行计算,有的只关注进程,对于超线程技术的线程来说,可能又是别的处理方式。对于多 CPU 的平均负载的计算,是在单 CPU 的情况下再除以 CPU 的个数。
文件: kernel/timer.c:
unsigned long avenrun[3]; | |
static inline void calc_load(unsigned long ticks) | |
{unsigned long active_tasks; /* fixed-point */ | |
static int count = LOAD_FREQ; | |
count -= ticks; | |
if (count < 0) {count += LOAD_FREQ; | |
active_tasks = count_active_tasks(); | |
CALC_LOAD(avenrun[0], EXP_1, active_tasks); | |
CALC_LOAD(avenrun[1], EXP_5, active_tasks); | |
CALC_LOAD(avenrun[2], EXP_15, active_tasks); | |
} | |
} |
内核中的函数 sched.h
/* | |
* These are the constant used to fake the fixed-point load-average | |
* counting. Some notes: | |
* - 11 bit fractions expand to 22 bits by the multiplies: this gives | |
* a load-average precision of 10 bits integer + 11 bits fractional | |
* - if you want to count load-averages more often, you need more | |
* precision, or rounding will get you. With 2-second counting freq, | |
* the EXP_n values would be 1981, 2034 and 2043 if still using only | |
* 11 bit fractions. | |
*/ | |
extern unsigned long avenrun[]; /* Load averages */ | |
extern void get_avenrun(unsigned long *loads, unsigned long offset, int shift); | |
extern unsigned long total_forks; | |
extern int nr_threads; | |
DECLARE_PER_CPU(unsigned long, process_counts); | |
extern int nr_processes(void); | |
extern unsigned long nr_running(void); | |
extern unsigned long nr_uninterruptible(void); | |
extern unsigned long nr_iowait(void); | |
extern unsigned long nr_iowait_cpu(int cpu); | |
extern unsigned long this_cpu_load(void); |
ewma 算法
linux 系统在很多方面都使用了这个算法,比如除了这个还有 TC 系统的 CBQ 算法,这是统计学的一个算法,具体请参考 http://en.wikipedia.org/wiki/EWMA_chart 和 http://en.wikipedia.org/wiki/Moving_average
参考文献
http://man.he.net/man8/tc-cbq-details
linux 内核代码
http://en.wikipedia.org/wiki/EWMA_chart
http://en.wikipedia.org/wiki/Moving_average
本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-05/116995.htm
