阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Unix平均负载average load计算方法

105次阅读
没有评论

共计 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);

#define FSHIFT          11              /* nr of bits of precision */
#define FIXED_1         (1<<FSHIFT)     /* 1.0 as fixed-point */
#define LOAD_FREQ       (5*HZ+1)        /* 5 sec intervals */
#define EXP_1           1884            /* 1/exp(5sec/1min) as fixed-point */
#define EXP_5           2014            /* 1/exp(5sec/5min) */
#define EXP_15          2037            /* 1/exp(5sec/15min) */

#define CALC_LOAD(load,exp,n) \
        load *= exp; \
        load += n*(FIXED_1-exp); \
        load >>= FSHIFT;

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

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计2499字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中