Today read the Linux source code on the CPU load calculation method, while searching the Google everywhere reference, dizzy for half a day, finally figure out how to calculate the CPU load, not a simple moving arithmetic average.
For Linux, sampling calculates the load interval of 5 seconds, which is a fixed number defined in source code, and its sampling structure can dynamically get timely data through the dynamic memory file system/proc/loadavg, and the output of other tools, such as Uptime/top /sar, etc. are all generated by reading the memory data. We are here to focus on kernel algorithms.
For a 5-second interval, the CPU state data is sampled dynamically, that is, the run queue size, which includes the number of processes that are running in the CPU and the number of processes in the CPU waiting queue. For Linux, it actually calculates a 1-minute, 5-minute, 15-minute moving average. To do this we first introduce the 3 constants defined in Linux:
We assume that the previous time the load calculated as constant 1884 is load1 (t-1), the current sample run queue size is rq1, then the current load1 (t) = ((Load1 (t-1) * 1884) + rq1 * (2048-1884))/2048
Similarly, the algorithm for 5-minute and 15-minute moving averages is load5 (t) = ((Load5 (t-1) *) + rq1 * (2048-2014))/2048 and load_15 (t) = ((Load15 (t-1) * 2037) + RQ 1 * (2048-2037))/2048
It can be seen that the larger the moving average interval, the smaller the effect of the current run queue size on the moving average tends to decrease.
As for why this number is involved in calculus, this makes the image smoother.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.