How Linux counts the CPU utilization of the process

Source: Internet
Author: User
Tags virtual environment

0. Why write this blog

The top or PS of Linux can see the CPU utilization of the process, so why do you need to know this detail? There are three reasons for writing this article:

* Want to be able to get process CPU utilization in a script in a "non-blocking" way * PS cannot get CPU utilization at the current time of the process; top takes at least 1 seconds to get the current utilization of the process * curiosity

1. How to count process CPU utilization1.0 Overview

In the Linux/proc file system, you can see the time slices that are consumed by all CPUs since start-up time, and for a process, you can also see the time slices consumed by the process. This is a cumulative value that can be "non-blocking" output. You can calculate the process CPU utilization during this time by two statistics for a certain time interval.

So, is there a simple, non-blocking way to get the CPU utilization of the process? The answer is: "No". Here's an interesting metaphor: "It's like someone gives you a picture of the speed of the car in your photo."

1.1/proc/stat Statistics Total CPU consumption

This concept is not important in calculations, but it is useful to know about it. In the/proc/[pid/]stat we can see the system statistics CPU time consumption, here are unified use 1/user_hz for a time slice (man proc), in most cases user_hz is the value of 100, so here is a time slice is 10ms. Sysconf (_SC_CLK_TCK) can be called by the system to obtain an accurate user_hz value.

For example:

# Cat/proc/stat|grep "CPU" CPU 77918485 720414 61184026 19052884316 12152363 1386 1476742 0 0

Each column corresponds to the CPU consumption meaning (man proc): User State (username), Low priority user State (Nice), System (SYS), idle, iowait (kernel 2.5.41+), interrupt (since 2.6.0+), soft terminal (since 2.6.0+), Steal (other OS consumed 2.6.11+ in the virtual Environment), guest (virtual CPU consumption 2.6.24 for guest OS)

Corresponds to the following:

# cat/proc/stat|grep "CPU" |usr |nice |sys |idle |iowait |irq |softirq |steal |guestcpu |77 918485 |720414 |61184026 |19052884316 |12152363 |1386 |1476742 | |

Therefore, the following shell command can be used to calculate the total CPU consumption:

Cat/proc/stat|grep "CPU" |awk ' {for (i=2;i<=nf;i++) j+= $i;p rint "Cpu_total_slice" J;} ' Cpu_total_slice 19208187744

In many places it is not complete to see just the previous four columns or five columns, but since the first four or five columns are usually the CPU's primary consumption, this calculation is usually accurate. For example, in the preceding example, the consumption of the previous five columns is 99.99% of CPU consumption.

(Tips: The jiffy of time slices and CPU clock interrupts here is not a concept, one is a kernel state, a user-state)

1.2 CPU time slices consumed by the process

In the proc file system, the time slice consumed by the process can be obtained through/proc/[pid]/stat, the 14th, 15, 16, and 17 columns of the output correspond to the CPU consumption of the process user, the consumption of the kernel State, the consumption of the user state waiting for the sub-process, and the consumption of the kernel state waiting for the subprocess (man proc). So the CPU consumption of the process can use the following command:

Cat/proc/9583/stat|awk ' {print ' Cpu_process_total_slice "$14+$15+$16+$17} ' Cpu_process_total_slice 1068099

Tips: From here you can see that Linux does not have process-level iowait statistics, and if you want to know which process is causing the iowait of your system, you will need some additional tool assistance.

1.3 "Non-blocking" calculation process CPU Utilization

It is also seen from here that there is no CPU utilization at some point, and there is no way to get CPU utilization at some point. This is like the concept of "speed" in physics, without the concept of a certain moment of speed, the speed must be within a period of time. Then to "non-blocking" to calculate the CPU utilization of a process, you need to take two event interval to calculate, this two event interval operation can be non-blocking. The calculation method is as follows:

* Moment A, calculate the operating system total CPU time slice consumption total_cpu_slice_a; Calculate the total CPU time slice consumption of the process; total_process_slice_a* time B, calculate the total CPU time slice consumption of the operating system Total_cpu_slice_b Calculation process total CPU time slice consumption; total_process_slice_b

B time can be "non-blocking" to calculate the CPU utilization of this time process:

100%* (total_process_slice_b-total_process_slice_a)/(TOTAL_CPU_SLICE_B-TOTAL_CPU_SLICE_A)

what does the 1.4 PS command show for CPU utilization?
Man PS ... CPU usage is currently expressed as the percentage of time spent running during the entire lifetime of a process. This is not a ideal, and it does not conform to the standards, which PS otherwise conforms to. CPU usage is unlikely to add-to-exactly 100% ....

As you can see, the PS command%CPU shows the total average CPU utilization up to the current time from the start of the process.

2 Reference Documents

* Man Proc/man PS

* Accurately calculating CPU utilization in Linux Using/proc/stat@stackoverflow

* @http://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/kernel/sched/core.c?id=head "target= "_blank" rel= "nofollow" >account_process_tick@linux Kernel

* Troubleshooting High I/O Wait in Linux

* Top and PS not showing the same CPU result


How Linux counts the CPU utilization of the process

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.