Top cpu usage principle and conflicts with real-time threads

Source: Internet
Author: User

In linux, you can use top or vmstat to view the system's CPU usage. In most cases, the CPU usage is accurate, but in real-time linux systems with high time precision, the CPU usage measured by top may be inaccurate. The following uses the top principle and real-time linux instances to analyze the usage of top in the real-time system.

When calculating the CPU usage, roughly count the following time:

User time, System time, Waiting time, Idle time, Nice time, and hardware terminal Processing time

It mainly calculates the user time, system time, and idle time. Basically, the sum of the percentages of these three times is 100%. We know that when top is used to calculate CPU usage, the top refresh frequency can be changed. By default, the top refresh frequency can be changed to 1 s or 0.1s, since the top refresh frequency can be changed, what is the highest refresh frequency of top, and how is the CPU usage calculated? In actual use, the data in the top table is obtained from the/proc/stat file. When the/proc/stat file is read, it is automatically updated to obtain the latest CPU usage, however, the/proc/stat file also has a refresh precision. The refresh precision is for each clock interruption period, that is, the data in the/proc/stat file can be updated once per tick at the fastest, when a clock is interrupted, the system checks the processes running on the current CPU. for user-State processes, a tick is added to the user-state record data in the kernel_stat data member. For kernel-State processes, the number of kernel-State member records increases. The top usage is derived from the statistics of each tick running process type of the current CPU. For example, the CPU usage of user time = tick of user process/total tick. Under normal circumstances, we know that linux uses the clock cycle as the basis for process scheduling. Basically, process scheduling does not occur in a clock cycle, so using top to count CPU usage will not go wrong.

However, in rt-linux, the precision of time granularity is no longer tick, but it may be smaller than tick. In this case, some processes or threads may occur, and their scheduling is more frequent, for example, in real-time linux with tick = 2 ms, there is a real-time process with a cycle of 1 ms. When we use top to count the CPU usage of the real-time process, the results may be inaccurate. As shown in, the current CPU runs a real-time process in 1 ms. Red indicates that the process is processing data, CPU is busy, and white indicates that the process is not processing data, and the CPU is idle. Because the real-time process and tick are both cyclical and the real-time process cycle is smaller than that of tick, the following two situations may occur. The first is the time when the real-time process is started. When the clock is interrupted, the current CPU usage is counted during the CPU busy, because tick is an integer multiple of the real-time process cycle, therefore, the clock interruption statistics always show that the CPU is busy. The result of this time is that the current CPU usage is 100%. In the second case, when the clock is interrupted, the current CPU usage is counted during the CPU idle time. The result is always that the CPU is idle, and the CPU usage is 0 when the CPU is on the top ,. As shown in the preceding example, when the granularity of linux is always smaller than that of tick, the top statistical result may be inaccurate.


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.