日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

linux主机load average的概念计算过程注意事项

發(fā)布時(shí)間:2024/4/14 linux 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux主机load average的概念计算过程注意事项 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? ? ? ? 最近開發(fā)的一個(gè)模塊需要根據(jù)機(jī)房各節(jié)點(diǎn)的負(fù)載情況(如網(wǎng)卡IO、load average等指標(biāo))做任務(wù)調(diào)度,剛開始對(duì)Linux機(jī)器load average這項(xiàng)指標(biāo)不是很清楚,經(jīng)過(guò)調(diào)研,終于搞清楚了其計(jì)算方法和影響因素,作為筆記,記錄于此。
1. load average
? ? ? ? 當(dāng)在shell終端鍵入top命令時(shí),默認(rèn)情況下,在輸出內(nèi)容的第一行會(huì)有l(wèi)oad average這項(xiàng)指標(biāo)值,如下所示:

top - 19:10:32 up 626 days, 4:58, 1 user, load average: 7.74, 5.62, 6.51 Tasks: 181 total, 8 running, 173 sleeping, 0 stopped, 0 zombie Cpu(s): 4.0% us, 0.5% sy, 0.0% ni, 95.4% id, 0.0% wa, 0.0% hi, 0.0% si

? ? ? ? 同樣,輸入uptime命令,load average也會(huì)被輸出:

19:15:10 up 129 days, 5:12, 15 users, load average: 0.01, 0.09, 0.05

? ? ? ? 根據(jù)man uptime的說(shuō)明可知, load average包含的3個(gè)值分別表示past 1, 5 and 15 minutes內(nèi)的系統(tǒng)平均負(fù)載。
? ? ? ? 那么,這3個(gè)值是怎么計(jì)算出來(lái)的?下面從Linux源碼中尋找答案。
2. linux機(jī)器load average的計(jì)算過(guò)程
? ? ? ? wikipedia在對(duì)load的解釋( 參見這里)中,提到了linux系統(tǒng)對(duì)load的計(jì)算方法,為親自驗(yàn)證,我check了linux源碼(linux kernel 2.6.9)中的相關(guān)代碼,自頂向下的驗(yàn)證過(guò)程如下。
? ? ? ? 在源碼樹kernel/timer.c文件中,計(jì)算系統(tǒng)load的函數(shù)代碼如下:

// 源碼樹路徑:kernel/timer.c /** Hmm.. Changed this, as the GNU make sources (load.c) seems to* imply that avenrun[] is the standard name for this kind of thing.* Nothing else seems to be standardized: the fractional size etc* all seem to differ on different machines.** Requires xtime_lock to access.*/ unsigned long avenrun[3];/** calc_load - given tick count, update the avenrun load estimates.* This is called while holding a write_lock on xtime_lock.*/ 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);} }

? ? ? ? 從上面的代碼可知,定義的數(shù)組avenrun[]包含3個(gè)元素,分別用于存放past 1, 5 and 15 minutes的load average值。calc_load則是具體的計(jì)算函數(shù),其參數(shù)ticks表示采樣間隔。函數(shù)體中,獲取當(dāng)前的活躍進(jìn)程數(shù)(active tasks),然后以其為參數(shù),調(diào)用CALC_LOAD分別計(jì)算3種load average。
? ? ? ? 沿著函數(shù)調(diào)用鏈,可以看到count_active_tasks()定義如下(也在kernel/timer.c文件中):

/* * Nr of active tasks - counted in fixed-point numbers*/ static unsigned long count_active_tasks(void) {return (nr_running() + nr_uninterruptible()) * FIXED_1; }

? ? ? ? 由源碼可見,count_active_tasks()返回當(dāng)前的活躍進(jìn)程數(shù),其中活躍進(jìn)程包括:1)當(dāng)前正在運(yùn)行的進(jìn)程(nr_running);2)不可中斷的sleeping進(jìn)程(如正在執(zhí)行IO操作的被掛起進(jìn)程)。
? ? ? ? 關(guān)于nr_running進(jìn)程和nr_uninterruptible進(jìn)程的計(jì)算方法,可以在源碼樹kernel/schde.c中看到相關(guān)代碼:

// 源碼樹路徑:kernel/sched.c /** nr_running, nr_uninterruptible and nr_context_switches:** externally visible scheduler statistics: current number of runnable* threads, current number of uninterruptible-sleeping threads, total* number of context switches performed since bootup.*/ unsigned long nr_running(void) {unsigned long i, sum = 0;for (i = 0; i < NR_CPUS; i++)sum += cpu_rq(i)->nr_running;return sum; }unsigned long nr_uninterruptible(void) {unsigned long i, sum = 0;for_each_cpu(i)sum += cpu_rq(i)->nr_uninterruptible;return sum; }

? ? ? ? 繼續(xù)沿著函數(shù)調(diào)用鏈查看,可在include/linux/sched.h中看到CALC_LOAD的定義:

// 源碼樹路徑:include/linux/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 */#define FSHIFT 11 /* nr of bits of precision */ #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */ #define LOAD_FREQ (5*HZ) /* 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;

? ? ? ? 可以看到,CALC_LOAD是一個(gè)宏定義,load average的值與3個(gè)參數(shù)相關(guān),但若只考慮某1項(xiàng)指標(biāo)值(如past 5 minutes的load average),則該值只受當(dāng)前活躍進(jìn)程數(shù)(active tasks)的影響,而活躍進(jìn)程數(shù)包括兩種:當(dāng)前正在運(yùn)行的進(jìn)程和不可中斷的掛起進(jìn)程。
? ? ? ? 這符合我的觀察結(jié)果:三臺(tái)硬件配置相同的linux機(jī)器(8 cup, 16GB memory, 1.8T disk),在當(dāng)前總進(jìn)程數(shù)相差不多(均為170+)的情況下,其中1臺(tái)機(jī)器有1個(gè)普通進(jìn)程(這里的"普通"是指既非CPU型又非IO型)在運(yùn)行,其余均sleeping;第2臺(tái)機(jī)器有5個(gè)cpu型進(jìn)程,cpu占用率均達(dá)到99%,其余進(jìn)程sleeping;第3臺(tái)機(jī)器2個(gè)進(jìn)程讀寫硬盤,其余sleeping。很明顯地可以看到:第3臺(tái)機(jī)器的load average指標(biāo)的3個(gè)值均為最大,第2臺(tái)機(jī)器次之,第1臺(tái)機(jī)器的3個(gè)值均接近0。
? ? ? ? 由此,還可以推斷出:與running類型的進(jìn)程相比,uninterruptible類型的進(jìn)程(如正在進(jìn)行IO操作)對(duì)系統(tǒng)load的影響較大。( 注:該推斷暫無(wú)數(shù)據(jù)或代碼支撐,若有誤,歡迎指正

3. 理解load average背后的含義
? ? ? ? 上面介紹了load average的概念及l(fā)inux系統(tǒng)對(duì)該指標(biāo)的計(jì)算過(guò)程,那么,這個(gè)指標(biāo)值到底怎么解讀呢?這篇文章給出了詳細(xì)且形象的說(shuō)明,此處不再贅述。

【參考資料】
1. wikipedia: Load (computing)?
2. linux源碼(內(nèi)核版本2.6.9)
3. Understanding Linux CPU Load - when should you be worried??

================== EOF ===================


?

總結(jié)

以上是生活随笔為你收集整理的linux主机load average的概念计算过程注意事项的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。