天天看點

Linux的load average的含義

下面文章中的      “  資料是每隔5秒鐘檢查一次活躍的程序數,然後根據這個數值算出來的。如果這個數除以CPU的數目,結果高于5的時候就表明系統在超負荷運轉了。”   

具體是什麼意思, 如果是cpu為8顆(雙核,4核不知道如何算),目前load average 為: 20.22,20.03,18.99 應該不算超負荷運作了 ?

======================================================

$ uptime

11:12:26 up 3:44, 4 users, load average: 0.38, 0.31, 0.19

上面的輸出,load average後面分别是1分鐘、5分鐘、15分鐘的負載情況。資料是每隔5秒鐘檢查一次活躍的程序數,然後根據這個數值算出來的。如果這個數除以CPU的數目,結果高于5的時候就表明系統在超負荷運轉了。其算法(摘自Linux 2.4的核心代碼)如下:

檔案: include/linux/sched.h:

#define FSHIFT 11 /* nr of bits of precision */

#define FIXED_1 (1#define LOAD_FREQ (5*HZ) /* 5 sec intervals */

#define EXP_1 1884 /* 1/exp(5sec/1min) as fixed-point, 2048/pow(exp(1), 5.0/60) */

#define EXP_5 2014 /* 1/exp(5sec/5min), 2048/pow(exp(1), 5.0/300) */

#define EXP_15 2037 /* 1/exp(5sec/15min), 2048/pow(exp(1), 5.0/900) */

#define CALC_LOAD(load,exp,n) \

load *= exp; \

load += n*(FIXED_1-exp); \

load >>= FSHIFT;

/**********************************************************/

檔案: 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 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);

}

檔案: fs/proc/proc_misc.c:

#define LOAD_INT(x) ((x) >> FSHIFT)

#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)

static int loadavg_read_proc(char *page, char **start, off_t off,

int count, int *eof, void *data)

int a, b, c;

int len;

a = avenrun[0] + (FIXED_1/200);

b = avenrun[1] + (FIXED_1/200);

c = avenrun[2] + (FIXED_1/200);

len = sprintf(page,"%d.%02d %d.%02d %d.%02d %ld/%d %d\n",

LOAD_INT(a), LOAD_FRAC(a),

LOAD_INT(b), LOAD_FRAC(b),

LOAD_INT(c), LOAD_FRAC(c),

nr_running(), nr_threads, last_pid);

return proc_calc_metrics(page, start, off, count, eof, len);

系統平均負載被定義為在特定時間間隔内運作隊列中的平均程序樹。如果一個程序滿足以下條件則其就會位于運作隊列中:

??- 它沒有在等待I/O操作的結果

??- 它沒有主動進入等待狀态(也就是沒有調用'wait')

??- 沒有被停止(例如:等待終止)

下面這個是算法,數學不好,看的不太懂,資料上是這麼說的\

load average怎麼計算?

   為 了使核心可以高效計算load average,采用了fixed-point arithmetic。fixed-point arithmetic是一種非常快速的模拟浮點運算的方法,特别是在沒有FPU(float point unit)部件的處理器上,非常有用。

計算公式:load(t) = load(t-1) e^(-5/60) + n (1 - e^(-5/60)),疊代計算,其中n為run-queue length。

為什麼采用這個計算公式呢?

由Exponential Smoothing方程有,Y(t)= Y(t-1) + a*[X(t) - Y(t-1)],whereX(t) is the input raw data, Y(t - 1) is the value due to the previoussmoothing iteration and Y(t) is the new smoothed value.

令a=1-b,b為e^(-5/60),就可以得到load average的計算公式

采用此公式的好處:局部的load抖動不會對load average造成重大影響,使其平滑。

繼續閱讀