Calculation of Linux CPU usage

Source: Internet
Author: User
Tags cpu usage

CPU usage is measured by the percentage of CPU that the program runs. CPU usage information for Linux can be calculated from the/proc/stat file.

Proc File system

The/proc file system is a pseudo-file system that only exists in memory and does not occupy external memory space. It provides the interface for the kernel to communicate with the process in a file system manner. Users and applications can get system information through/PROC, and can change certain parameters of the kernel. Because the information of the system, such as the process, is dynamically changed, so when a user or application reads a file from the/proc directory, the proc file system dynamically reads the required information from the system kernel and submits it.


There are some directory names in the/proc directory, which are the process directories. Each process currently running in the system corresponds to a directory/proc/pid that is the directory name of the process number under/proc, which is the interface that reads the process information. In addition, there is a task directory in the/proc/pid directory in the version above Linux 2.6.0-test6, and there are some directory/proc/pid/task/tid named for the thread number of threads owned by the process/proc/pid/task directory. They are interfaces that read thread information.

/proc/cpuinfo file

This file contains information about the CPU (model number, cache size, etc.).

/proc/stat file

The file contains information about all CPU activity, and all values in the file are accumulated from the start of the system to the current moment. The format of the file may be inconsistent in different kernel versions, and the following examples illustrate the meaning of each field in the data file.

The number on the first line represents the total CPU usage, so we just use the first line of numbers to calculate it. The following table resolves the meanings of the first row of values:

Here the units of the numbers are: jiffies
Jiffies is a global variable in the kernel that records the number of Beats generated from the start of the system, and in Linux, a beat can be roughly understood as the minimum time slice for operating system processes, and different Linux cores may have different values, usually between 1ms and 10ms.

Resources:

CPU usage calculation for Linux platforms
Http://www.blogjava.net/fjzag/articles/317773.html

Calculation of Linux CPU usage

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.