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<
 112 #define LOAD_FREQ   (5*HZ)      /* 5 sec intervals */
 113 #define EXP_1       1884        /* 1/exp(5sec/1min) as fixed-point */
 114 #define EXP_5       2014        /* 1/exp(5sec/5min) */
 115 #define EXP_15      2037        /* 1/exp(5sec/15min) */

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


  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;
1949
1950 for_each_online_cpu(i) {
1951 running += cpu_rq(i)->nr_running;
1952 uninterruptible += cpu_rq(i)->nr_uninterruptible;
1953 }
1954
1955 if (unlikely((long)uninterruptible < 0))
1956 uninterruptible = 0;
1957
1958 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上的活動進程數決定的,但並不是在數值上等同於每秒鐘需要進行調度的進程數,具體的計算過程是個比較複雜的過程。
 原文地址 [url]http://linux.ccidnet.com/art/302/20070718/1148125_1.html[/url]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章