How to obtain cpu utilization in linux

Source: Internet
Author: User
Article Title: how to obtain cpu utilization in linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

"The proc file system is a pseudo file system, which only exists in the memory and does not occupy the external storage space. It provides interfaces for accessing system kernel data in the form of a file system. Users and applications can obtain system information through proc and change certain kernel parameters. "

This section describes how to obtain some firewall-related performance parameters from the/proc file system and how to modify the Kernel configuration through the/proc file system.

1. Obtain relevant performance parameters from the/proc file system

Cpu usage:/proc/stat

Memory usage:/proc/meminfo

Network load information:/proc/net/dev

Calculation method: (from: proc file system, see references)

(1) processor usage

(2) memory usage

(3) inbound and outbound data packets

(4) overall network load

These data must be extracted from the/proc/stat,/proc/net/dev, And/proc/meminfo files respectively. If you have any problems or are not clear about the data to be extracted, you can use man proc to view the online manual of the proc file system.

(1) processor usage

Here, we need to extract four pieces of data from/proc/stat: user mode, nice mode, and system mode) and idle processor time (idle ). They are located in the first line of the/proc/stat file. The CPU usage is calculated using the following formula.

CPU usage = 100 * (user + nice + system)/(user + nice + system + idle)

(2) memory usage

Here we need to extract two data from the/proc/meminfo file, the current memory usage (cmem) and the total memory (amem ).

Memory usage percentage = 100 * (cmem/umem)

(3) Network Utilization

To obtain data about network utilization, You need to obtain two pieces of data from the/proc/net/dev file: the number of data packets output from the local machine and the number of data packets flowing into the local machine. Both are located in the fourth row of the file.

The performance collection program starts to record the initial values of the two data values. After each value is obtained, the initial value is subtracted from the data packets passed through the current node starting from the cluster.

Calculate the average network load by using the above data:

Average network load = (output packet + incoming packet)/2

2. Adjust the related Kernel configuration through the/proc file system

Allow ip Forwarding/proc/sys/net/ipv4/ip_forward

Disable ping/proc/sys/net/ipv4/icmp_echo_ignore_all

You can directly write "1" to the above two "Files" under the command line to implement related configuration. If "0" is written, the related configuration will be canceled. However, after the system is restarted, these configurations will be restored to the default settings. If you want these modifications to take effect, you can directly write the following configuration into the/etc/profile file, or other program files executed as the system starts.

Echo 1>/proc/sys/net/ipv4/ip_forward

Echo 1>/proc/sys/net/ipv4/icmp_echo_ignore_all

If you need to obtain other performance parameters or perform more kernel configurations, you can refer to the proc file system introduction in the following link, or you can directly view related information through man proc.

References and links

[1] What is a proc file system?

Http://blog.chinaunix.net/u1/37836/showart_304248.html

[2] How to obtain cpu usage in Linux

Http://topic.csdn.net/t/20060701/23/4855045.html

[3] indexing the/proc/net/Directory

Linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html? Page = 1 "> http://www.linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html? Page = 1

[4]/proc/net introduction

Http://www.redhat.com/docs/manuals/enterprise/RHEL-4-Manual/en-US/Reference_Guide/s2-proc-dir-net.html

[5] Using ulimit and proc to adjust system parameters

Http://www.linuxfly.org/post/73.htm

Note: The file contains an increment. The CPU usage for each item corresponding to a time interval increases. to calculate the percentage, you must use the last status value.

Here, there is a problem in calculating the cpu usage. You need to use the value in the previous state to calculate the cpu usage.

Return

Elf was published on #4

The correct calculation method is to wait for the previous time:

1. Record cpu usage at a specific time point

2. Wait for the previous time period

3. Record the current cpu usage

4. Calculate the total time slice

Sum all cpu usage for the first time and obtain j1

Sum up all cpu usage for the second time and obtain j2

J2-j1 gets all time slices for this time period

That is, total = j2-j1 = sum of all columns for the second time-sum of all columns for the first time

5. Calculate the idle time

Idle corresponds to the data in the Fifth Column. Use the second value minus the first value.

Idle = fifth column of the second operation-fifth column of the first operation

6. Calculate cpu usage

Rate = (total-idle)/total

See http://oss.lzu.edu.cn/blog/article.php? Tid_1379.html

When we conduct performance tests, it is a common method to monitor the CPU usage of backend servers. If the CPU usage of the server is high, the server is busy. If the front-end response time is getting bigger and bigger, and the back-end CPU utilization is always not going up, it means there is a bottleneck somewhere and the system needs to be tuned. This is something that is easily understood by people who do not know the technology.

Is that correct? I personally think it is not very accurate. This depends on the type of processes you test in the background. If it is a computing-intensive process, when the front-end pressure increases, it is easy to put the CPU utilization rate up. However, if it is an I/O network-intensive process, even if there are more and more client requests, but the server CPU may not be able to go up, this is determined by the natural attributes of the process you want to test.

What is CPU utilization? In Linux/Unix, CPU utilization is divided into user-state, system-state, and idle state, indicating the time when the CPU is in user-state, and the time when the system kernel is executed, and the execution time of idle system processes. Since the computer power-on, the CPU has been busy, so the CPU utilization is always 100%. When no user process needs to be executed, the CPU executes the default idle process of the system. The CPU usage refers to the time when the CPU executes non-system idle processes/The total execution time of the CPU.

In the Linux kernel, there is a global variable: Jiffies. Jiffies indicates the time. Its unit varies with the hardware platform. A constant HZ is defined in the system, representing the number of minimum time intervals per second. In this way, the unit of jiffies is 1/HZ. Jiffies of Intel Platform is measured in 1/100 seconds, which is the minimum time interval that the system can distinguish. For each CPU time slice, Jiffies must be added with 1. CPU utilization is expressed by dividing the execution user State + system state Jiffies by the total Jifffies.

In Linux, the CPU utilization is calculated from the/proc/stat file. the first few lines of this file record the user State and system state of each CPU, for different Jiffies in idle state, common monitoring software uses the data in/proc/stat to calculate the CPU utilization.

Contains information about all CPU activities. All values in this file are accumulated from the start of the system to the current time.

/Proc/stat/

[Work @ builder ~] $ Cat/proc/stat

Cpu 432661 13295 86656 422145968 171474 233

Cpu0 123075 2462 23494 105543694 0 16586

Cpu1 111917 4124 23858 105503820 69697 123

Cpu2 103164 3554 21530 105521167 64032 106 334

Cpu3 94504 3153 17772 105577285 4 24

Intr 1065711094 1057275779 92 0 6 0 4 0 0 0 0 0 0 70 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7376958 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Ctxt 1, 19067887

(Btime 1139187531)

Processes 270014

Procs_running 1

Procs_blocked 0

Output description

The meanings of each parameter in each line of CPU and CPU0, CPU1, CPU2, and CPU3 (in the first behavior example) are:

Parameter description

User (432661) indicates the CPU time (unit: jiffies) of the user State from the start of the system to the current time. The nice value is not a negative process. 1 jiffies = 0.01 seconds

Nice (13295) indicates the CPU time occupied by processes whose nice value is negative (unit: jiffies) from the time the system starts to the current time)

System (86656) accumulates from system startup to current time, core time (unit: jiffies)

Idle (422145968) accumulates from system startup to current time, except hard disk IO wait time (unit: jiffies)

Iowait (171474) accumulates from system startup to current time, hard disk IO wait time (unit: jiffies ),

Irq (233) indicates the hard interrupt time (unit: jiffies) from the start of the system to the current time)

Softirq (5346) indicates the Soft Interrupt time (unit: jiffies) from the start of the system to the current time)

CPU time = user + system + nice + idle + iowait + irq + softirq

The "intr" line shows the number of interruptions. The first line is the number of all interruptions that have occurred since the system was started; then each number corresponds to the number of times a specific interruption has occurred since the system was started.

"Ctxt" indicates the number of times the CPU context has been exchanged since the system was started.

"Btime" indicates the time (in seconds) that has elapsed since the system was started.

The number of tasks created since the system started "processes (total_forks.

"Procs_running": number of tasks in the current queue.

"Procs_blocked": Number of blocked tasks.

So the CPU usage calculation method: You can use two sampling points to calculate the difference.

(Idle2-idle1)/cpu2-cpu1

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.