Linuxloadavg algorithm. Author of The linuxloadavg algorithm: biti_rainy today, I read linuxsourcecode's cpuload calculation method and searched for it everywhere on google. after a long time, I finally figured out the cpuloa linux loadavg algorithm.
Published by: biti_rainy
Today, I read the linux source code about the cpu load calculation method and searched for reference everywhere on google. after a long time, I finally figured out the cpu load calculation method, it is not a simple moving arithmetic mean.
For linux, the sampling and computing load interval is 5 seconds, which is a fixed number defined in the source code, its sampling structure can dynamically obtain timely data through the dynamic memory file system/proc/loadavg. the output of other tools, such as uptime/top/sar, is generated by reading the memory data. We will focus on the kernel algorithm here.
For a 5-second interval, the cpu status data is dynamically sampled, that is, the run queue size, which includes the number of running processes in the cpu and the number of processes in the cpu wait queue. In linux, the moving average of 1 minute, 5 minutes, and 15 minutes is calculated. First, we will introduce the three constants defined in linux:
# Define EXP_1 1884/* 1/exp (5sec/1 min )*/
# Define EXP_5 2014/* 1/exp (5sec/5 min )*/
# Define EXP_15 2037/* 1/exp (5sec/15 min )*/
The three constants represent constants of 1/5/15 minutes respectively. the calculation method is as follows:
1884 = 2048/(power (e, (5/(60*1)/* e = 2.71828 */
2014 = 2048/(power (e, (5/(60*5 ))))
2037 = 2048/(power (e, (5/(60*15 ))))
We assume that the load calculated by constant 1884 is load1 (T-1) at the previous time, and the current sample run queue size is rq1, then the current load1 (t) = (load1 (T-1) * 1884) + rq1 * (2048-1884)/2048
Similarly, the algorithms for moving the average of 5 minutes and 15 minutes are load5 (t) = (load5 (T-1) * 2014) + rq1 * (2048-2014), respectively )) /2048 and load_15 (t) = (load15 (T-1) * 2037) + rq1 * (2048-2037)/2048
It can be seen that the larger the moving average interval, the influence of the current run queue size on the moving average tends to decrease.
As for why this number is used, it involves calculus knowledge, so that the image is smoother.
Http://www.bkjia.com/PHPjc/532000.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/532000.htmlTechArticlelinux loadavg algorithm Published by: biti_rainy today read linux source code on the cpu load calculation method, at the same time on google search reference everywhere, dizzy for a long time, finally understand the cpu loa...