The CPU usage of vmstat, top, and PS aux is inconsistent)

Source: Internet
Author: User
Problem:
Run the vmstat, top, and PS aux commands to view the CPU usage of the process. The value varies greatly.

Example:
Use vmstat to view the system's CPU idle rate. The ID is the CPU idle rate. It can be seen that the idle rate is always above 73%.
Use PS aux to count the CPU usage of two compress processes. The CPU usage remains constant at 15.5% and 28.9% for a long time.
Using Top to calculate the CPU usage of two compress processes, we can see that it is a dynamic process
(The command execution result is displayed later)

The CPU usage detected by these three commands is inconsistent
First, two compress processes in PS aux occupy 15.5% + 28.9%> 40% of the CPU.
He vmstat can check whether the CPU idle rate ID can be greater than 73%
Second, the CPU usage of the compress process displayed by the top command is significantly different from the value displayed by the ps command.
The CPU usage remains unchanged and remains a constant value,
Top is relatively dynamic. Is the CPU usage of processes displayed by PS aux unreliable?
Finally, the CPU usage detected by these three commands is inconsistent with each other. Which one is more reliable? For example, enter the following command, which indicates the CPU usage in bold.

$ Vmstat 1
Procs ----------- memory ---------- --- swap -- ----- Io ---- System -- ---- CPU ----
R B SWPD free buff cache Si so Bi Bo in CS us Sy ID wa
0 0 432 802660 28724 0 5747324 204 0 0 6 3902
3 0 432 799196 28724 5747392 0 0 152 1455 2960
0 0 432 802420 28724 5747460 0 0 480 1599 10 882
0
0 0 432 802292 28724 5747664 0 0 440 1859 14 1373
0
2 0 432 797636 28724 5752084 0 0 240 1403 8 7850
1 0 432 798732 28724 5751268 0 0 448 1506 4608 10 8810
2 0 432 797924 28724 5752084 0 0 192 1752 11 10790
0 0 432 802076 28724 5747936 0 0 280 1705 14 679
0

$ PS aux | grep Compress
User PID % CPU % mem vsz RSS tty stat Start Time Command
IMG 656915.50.2 36716 16540? S aug12 230: 57./bin/compress 8014
IMG 657028.90.2 54592 22524? S Aug 12 430: 28./bin/compress 8004

$ Top | grep Compress
PID user PR Ni virt res shr s % CPU % mem time + command
6569 IMG 16 0 40956 20 m 6632 R10.70.3 231: 10.12 Compress
6570 IMG 16 0 54592 21 m 2456 S20.30.3 430: 54.32 Compress
6569 IMG 17 0 36740 16 m 2456 S10.30.2 231: 10.43 Compress
6570 IMG 16 0 54592 21 m 2456 R58.90.3 430: 56.09 Compress
6569 IMG 16 0 36716 16 m 2456 R53.90.2 231: 12.05 Compress
6570 IMG 16 0 58444 24 m 4752 R69.90.3 430: 58.19 Compress
6569 IMG 16 0 36716 16 m 2456 S60.20.2 231: 13.86 Compress
6570 IMG 16 0 54592 21 m 2456 S52.60.3 430: 59.77 Compress
6569 IMG 15 0 36716 16 m 2456 S45.30.2 231: 15.22 Compress

I have read some source code to add the following:
Top, PS, and vmstat are calculated based on the system's proc files. Among them, vmstat and top all use the/proc/STAT file, while PS, top also uses the/proc/*/STAT file for calculation.

1. vmstat is complex in calculating the CPU idle, but the average value is displayed, that is, the difference between the time slice used by the CPU, the time interval, and the number of CPUs (the number of physical CPUs * The number of CPU cores ), the interval is the parameter added after vmstat. For example, vmstat 10 indicates the average CPU usage within 10 s;

2. PS calculates the CPU usage time of each process. It uses the/proc/*/STAT file, where * is the process number, and the calculation method is the total number of time slices of CPU/hertz, currently, it is determined that the CPU usage rate is for each physical CPU. Therefore, the value displayed on the dual-core machine must be divided by the number of cores 2;

3. Top uses/proc/STAT to calculate the CPU usage shown above. Use/proc/*/STAT to calculate the time occupied by each process. However, the calculation method is different and the code is complicated. However, it is confirmed that the default mode is Irix, that is, the time occupied by a single CPU core, if the dual-core machine needs to divide the value by 2*2, Shift + I can switch;

The above instructions can be used to explain the problems found by the publisher:
Question 1: first, two compress processes in PS aux occupy 15.5% CPU + 28.9%> 40%. In addition to other processes, why does vmstat check whether the CPU idle rate ID can be greater than 73%?
The CPU usage of PS needs to be divided by the number of dual-core CPUs to 2, so the actual usage is (15.5% + 28.9%)/2 = 22.2%, which is about equal to 100%-73%;

Problem 2: Second, the CPU usage of the compress process displayed by the top command is significantly different from the value displayed by the ps command. The CPU usage of the PS remains constant and the top command is dynamic, is the CPU usage of processes displayed by PS aux unreliable?
The difference between PS and top is two times, so the displayed usage is very different, and the change rate is also large;

Question 3: At last, the CPU usage detected by these three commands is inconsistent with each other. Which one is more reliable? For example, enter the following command, which indicates the CPU usage in bold.
The usage of these three commands is unified, and the data for calculation is consistent according to the following formula:
Vmstat 100%-idle = Ps CPU usage/number of CPU cores = Top CPU usage/number of CPU cores/number of physical CPUs;

From: http://blog.163.com/double_tro/blog/static/204851237201241233843303/

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.