LINUXLOADAVG algorithm _php

Source: Internet
Author: User
Keywords Algorithms calculations minutes linux cpu moving
Linux LOADAVG algorithm


Posted by: Biti_rainy



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:

#define EXP_1 1884/* 1/EXP (5sec/1min) */
#define EXP_5 */1/exp (5sec/5min) */
#define EXP_15 2037/* 1/EXP (5sec/15min) */

The three constants represent constants of 1/5/15 minutes, respectively, and are calculated by:

1884 = 2048/(Power (E, (5/(60*1)))/*/E = 2.71828 */

= 2048/(Power (E, (5/(60*5))))

2037 = 2048/(Power (E, (5/(60*15))))

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.
  • Related Article

    Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.