linuxloadavg算法
linux loadavg 算法
发表人:biti_rainy
今天读linux source code关于cpu load 的计算方法,同时在google上搜索到处参考,晕乎了半天,终于弄明白cpu load 的计算方法了,并不是简单的移动算术平均。
对于linux来说,采样计算load时间间隔为5秒,这都是在source code里面定义的固定数字,其采样结构通过动态内存文件系统 /proc/loadavg 可以动态的得到适时数据,其他工具的输出,比如 uptime / top/sar 等都是读该内存数据所产生的。我们在这里主要考究kernel的算法。
对于5秒的间隔,是动态地采样cpu状态数据,也就是run queue size ,这包括正在cpu中running的进程数量以及在cpu等待队列里面的进程数量。对于linux来说,实际上会计算1分钟、5分钟、15分钟的移动平均。为此首先我们要介绍linux 里面定义的3个常量:
#define EXP_1 1884 /* 1/exp(5sec/1min) */
#define EXP_5 2014 /* 1/exp(5sec/5min) */
#define EXP_15 2037 /* 1/exp(5sec/15min) */
三个常量分别表示1/5/15分钟的常量,计算方法是:
1884 = 2048/(power(e,(5/(60*1)))) /* e = 2.71828 */
2014 = 2048/(power(e,(5/(60*5))))
2037 = 2048/(power(e,(5/(60*15))))
我们假定前一时刻按常量1884计算的load为 load1(t-1),当前采样run queue size 为 rq1,则当前load1(t) = ((load1(t-1) * 1884) + rq1 * (2048 - 1884))/2048
同理可以5分钟和15分钟移动平均的算法分别为 load5(t) = ((load5(t-1) * 2014) + rq1 * (2048 - 2014))/2048和load_15(t) = ((load15(t-1) * 2037) + rq1 * (2048 - 2037))/2048
由此可以看出,移动平均间隔越大,当前run queue size 对移动平均的影响趋向减小。
至于为什么取这个数,涉及到微积分方面的知识了,这样做出的图象更平滑。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...
