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