Linux Statistics Process CPU Utilization command detailed

Source: Internet
Author: User
Tags time interval cpu usage

1.0 Overview
In the Linux/proc file system, you can see the time slices that start at startup, all CPU consumption, and the time slices that the process consumes for a process. This is a cumulative value that can be "non-blocking" output. The process CPU utilization during this time can be calculated by obtaining two statistics of a certain time interval.

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

1.1/proc/stat Statistics Total CPU consumption
This concept is not important in computing, but it is useful to know. In/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 a value of 100, so here's a time slice is 10ms. The exact user_hz value can be obtained by system call sysconf (_SC_CLK_TCK).

For example:

The code is as follows Copy Code
# 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 consumption 2.6.11+ in virtual Environments), guest (running virtual CPU consumption for guest OS 2.6.24)

Corresponds to the following:

The code is as follows Copy Code
# cat/proc/stat|grep ' CPU '
|USR |nice |sys |idle |iowait |irq |softirq |steal |guest
CPU |77918485 |720414 |61184026 |19052884316 |12152363 |1386 |1476742 |

Therefore, the total CPU consumption can be computed using the following shell command:

The code is as follows Copy Code
Cat/proc/stat|grep "CPU" |awk ' {for (i=2;i<=nf;i++) j+= $i;p rint ' Cpu_total_slice ' j;} '
Cpu_total_slice 19208187744

It's not complete in many places to just count the first four or five columns, but because usually the top four or five columns are the CPU's main consumption, so this calculation is usually accurate. For example, in the previous example, the consumption of the previous five columns was 99.99% of CPU consumption.

(Tips: Here's the time slice and CPU clock interrupt jiffy is not a concept, one is the kernel state, a user state)

1.2 CPU time slices consumed by the process
In the proc file system, the process consumption time slice can be obtained by/proc/[pid]/stat, the output 14th, 15, 16, 17 columns corresponding to the process user state CPU consumption, kernel state consumption, User state wait child process consumption, kernel state wait child process consumption (man proc). So the CPU consumption of the process can use the following command:

The code is as follows Copy Code
Cat/proc/9583/stat|awk ' {print ' Cpu_process_total_slice ' $14+$15+$16+$17} '
Cpu_process_total_slice 1068099tips

: As you can see from here, Linux does not have process-level iowait statistics, and if you want to know which process is causing the system's iowait, then you need some additional tool support.

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 speed at some point, the speed must be within a period of time. To "non-blocking" compute the CPU utilization of a process, you need to take two event intervals to compute, and the operations of the two event intervals can be non-blocking. The calculation method is as follows:

* Moment A, calculates the total CPU time slice consumption total_cpu_slice_a of the operating system, calculates the total CPU time slice consumption of the process; total_process_slice_a
* Moment B, calculates the total CPU time slice consumption total_cpu_slice_b of the operating system, calculates the total CPU time slice consumption of the process; Total_process_slice_bb time can be "non-blocking" to compute the CPU utilization of this time process:

The code is as follows Copy Code
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 CPU utilization mean

The code is as follows Copy Code
Mans PS
......
CPU usage is currently expressed as the percentage of time spent running during the entire of a process. This is
Not ideal, and it does not conform to the standards which PS otherwise to. CPU usage is unlikely to add
Exactly 100%.
......

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

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.