Linux操作系统中关于负载的定义

使用uptime或者top命令,都可以看到一个负载的输出,形如load average: 0.00, 0.03, 0.00,这个负载到底是什么东西呢,man文档里只是一笔带过,没有具体的给出负载的定义。  负载的统计,必然是由内核完成的,因此在内核源码中找答案是再好不过的事情了,找来2.6.21的内核源码,开始探索。  节选部分源码: 

//kernel/timer.c

1254 active_tasks = count_active_tasks();1256 CALC_LOAD(avenrun[0], EXP_1, active_tasks);1257 CALC_LOAD(avenrun[1], EXP_5, active_tasks);1258 CALC_LOAD(avenrun[2], EXP_15, active_tasks);

//include/linux/sched.h

110 #define FSHIFT 11 /* nr of bits of precision */111 #define FIXED_1 (1<

  load(t) = ( load(t-1)*exp(i) + n(t)*(2048-exp(i)) ) / 2048

  load(t-1)为上次计算出的结果

  n(t)为t时刻的活动进程数

  计算方式是累加各个CPU的运行队列中running和uninterruptible的值 再乘以2048

  计算方式如下:

1946 unsigned long nr_active(void)1947 {1948 unsigned long i, running = 0, uninterruptible = 0;19491950 for_each_online_cpu(i) {1951 running += cpu_rq(i)->nr_running;1952 uninterruptible += cpu_rq(i)->nr_uninterruptible;1953 }19541955 if (unlikely((long)uninterruptible < 0))1956 uninterruptible = 0;19571958 return running + uninterruptible;1959 }1226 static unsigned long count_active_tasks(void)1227 {1228 return nr_active() * FIXED_1;1229 }

  exp(1) = 1884  exp(5) = 2014  exp(15) = 2037  exp(i) = 2048 * e^(-1/12/i)  从本质上看负载是完全由过去的一段时间里每个CPU上的活动进程数决定的,但并不是在数值上等同于每秒钟需要进行调度的进程数,具体的计算过程是个比较复杂的过程。

正如我总是意犹未尽的想起你。

Linux操作系统中关于负载的定义

相关文章:

你感兴趣的文章:

标签云: