Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

One, foreword

Today in the 7DGroup group, someone raised a question, why ps statistics of CPU percentage is much less than top statistics?

Second, problem phenomenon

The diagram below:

Add it up from the picture above. It’s a big difference.

Top:


800 16.9 7.6 22.1 29.9 8.8 24.4 16.9 20.3 = 653.1 800-16.9-7.6-22.1-29.9-8.8-24.4-16.9-20.3 = 653.1

In PHOTOSHOP, it’s less than 300%.

Third, problem analysis

What does that mean?

First, we need to understand the difference between PS and TOP:

  • Top is a monitoring tool, but PS is a snapshot tool. This is an essential difference.

  • Ps is to fetch the current data from the /proc/pid directory. Top is always fetching data and calculating according to the refresh cycle.

  • How does PS calculate the CPU?

    • The parameters are as follows:
      • System startup time: indicates the total time since the system was started.
      • Thread start time: The point at which a thread is started.
      • Thread CPU time: The length of time a thread uses the CPU.
      • Thread time = System start time – Thread start time
      • Thread CPU usage = Thread CPU time x 1000 / thread time
      • Calculated percentage of CPU usage = Thread CPU usage / 10. Thread CPU usage %10
    • For example:
      • System startup time: 15456374.085712
      • Thread start time: 9470058.848042
      • Thread CPU time: 987163
      • Thread time = 15456374.085712-9470058.848042 = 5986315.23767
      • Thread CPU usage = 987163 * 1000/5986315.23767 = 164.9
      • Calculated CPU usage = 164.9/10. 164.9%10 = 16.5
  • Ps calculates the percentage data from the /proc/directory

    • Retrieve the following times from /proc/pid/stat:
      • Utime: CPU time consumed in user mode
      • Stime: CPU time consumed in kernel mode
      • Cutime: CPU time consumed in user mode, including child processes
      • Cstime: CPU time consumed in kernel mode, including child processes
      • Starttime: indicates the starttime of the thread
  • Time consumption is measured by CPU time slices. The calculation is based on the CPU time slice, which is the number of CPU calculations per second.

Now we’re just going to do the calculation from the values that we took out.

If you want to use PS to calculate the CPU usage in a certain period, you can do as follows:

[root@7dgroup 2287]# ps -p 2287 -o %cpu,cputime,etime,etimes%CPU TIME ELAPSED ELAPSED 0.3 00:00:00 01:0363Copy the code

There are two parameters, etime and etimes (header is ELAPSED), which are different:

  • Etime: duration of a thread since it started. The format is [DD-]hh:]mm:ss.

  • Etimes: Duration of a thread since it started, in seconds.

For example, the 2287 process above is evaluated for the first time as shown above.

To calculate how much CPU was used up to the current time, we need to fetch the data again:

[root@7dgroup 2287]# ps -p 2287 -o %cpu,cputime,etime,etimes
%CPU     TIME     ELAPSED ELAPSED
 0.3 00:00:01       04:30     270
Copy the code

Calculate the time:

Previous value:

  • The CPU time slice consumption is 0. The calculation process is :(003600+0060+00)

  • The CPU time window is 63. The calculation process is: 1*60+3 = 63.

The last value:

  • The CPU slice consumption is: 1. The calculation process is: 003600+0060+1

  • The CPU time window is 270. The calculation is: 4*60+30 = 270.

The CPU usage calculation is as follows:


( ( 1 0 ) / ( 270 63 ) ) 100 = 0.4 ((1-0)/(270-63)) * 100 = 0.4

So the percentage of CPU used by this process during this time is 0.4%.

Interested also can be stroked ps source code:

If there is an objection to the top calculation results, you can also go to the source code of top.

Four,

So when we look at the resource utilization of a system, should we look at PS or top?

The relationship between PS and TOP is as follows:

Top indicates the resource statistics within a period. Ps is the resource value of each point in time.

To monitor the overall resource usage of a system, top is recommended.

If you want to analyze the CPU resources used by a specific thread, use Ps.