The relationship and difference between CPU usage and machine load under Linux

When we use the top command to view the resource usage of the system, we will see the load average, as shown in the following figure. It represents the average workload of the system at 1, 5, 15 minutes. So what is a load? What does it have to do with CPU utilization?


Load average: The average load of the system is the Load of the CPU. The information it contains is not the utilization rate of the CPU, but the statistics of the sum of the number of processes that the CPU is processing and waiting for the CPU to process over a period of time, that is, the CPU usage queue. length statistics. The lower the number, the better.

1. The difference between CPU load and CPU utilization

CPU Utilization : Shows the percentage of CPU occupied by the program in real-time during running.

CPU Load : Shows the average number of tasks that are using and waiting to use the CPU over a period of time. High CPU utilization does not mean that the load must be large. For example: if I have a program that needs to use the computing function of the CPU all the time, then the utilization rate of the CPU may reach 100% at this time, but the workload of the CPU is close to "1", because the CPU is only responsible for one job Well! What if two such programs are executed at the same time? The CPU usage is still 100%, but the workload has changed to 2. So that is to say, when the workload of the CPU is larger, it means that the CPU must perform frequent work switching between different tasks.

For example

, there is an article on the Internet that uses an interesting analogy to illustrate the difference between the two by making a phone call. I will explain it according to my own understanding.

In a public telephone booth, one person is calling, and four people are waiting. Each person is limited to using the phone for one minute. If someone does not finish the call within one minute, they can only hang up and wait in line for the next round. The phone is the CPU here, and the number of people who are calling or waiting to be called is the number of tasks.

During the use of the phone booth, some people will definitely leave after calling, some people choose to queue up again without finishing the call, and there will be new people queuing here. The change of this number is equivalent to the increase or decrease of the number of tasks. In order to count the average load, we count the number of people every 5 minutes, and average the statistics at the 1st, 5th, and 15th minutes to form the average load of the 1st, 5th, and 15th minutes.

Some people pick up the phone and make a call, and keep calling for 1 minute, while some people may be looking for a phone number for the first 30 seconds, or hesitating whether to call, and only make a real call after 30 seconds. If the phone is regarded as the CPU and the number of people is regarded as the task, we say that the CPU utilization of the former person (task) is high, and the CPU utilization of the latter person (task) is low.

Of course, the CPU does not work for the first 30 seconds and rests for the next 30 seconds. It just means that some programs involve a lot of calculations, so the CPU utilization is high, while some programs involve very little calculation. , the CPU utilization is naturally low. But no matter whether the CPU utilization is high or low, it has nothing to do with how many tasks are queued later.

2. What is the ideal load?

This is controversial, and each has its own statement. I personally agree that the CPU load less than or equal to 0.5 is an ideal state.

No matter how good a certain CPU is, how many tasks can be processed in 1 second, we can consider it to be irrelevant, although it is not. When evaluating CPU load, we only count task queue lengths in units of 5 minutes. If the task queue length is 1 every 5 minutes, the CPU load is 1. If we only have a single-core CPU, the load is always 1, which means that no tasks are queued, which is not bad.

But my server has a dual-core CPU, which means it has 4 cores. If the load of each core is 1, the total load is 4. That said, if my server's CPU load stays around 4 for a long time, it's okay.

But a load of 1 per core is not an ideal state! This means that our CPU is always busy and should not be idle. It is said on the Internet that the ideal state is that the load of each core is about 0.7. I agree with it. Multiply 0.7 by the number of cores to get the ideal CPU load of the server. For example, on my server, the load is below 3.0.

3. How to reduce the CPU load of the server?

The easiest way is to replace the server with better performance. Don't think about only improving the performance of the CPU. It is useless. The CPU needs the cooperation of other hardware and software to play its best performance.

In the case of reasonable configuration in other aspects of the server, the number of CPUs and the number of CPU cores (ie, the number of cores) will affect the CPU load, because tasks are ultimately allocated to the CPU cores for processing. Two CPUs are better than one, and dual cores are better than single cores.

So, we need to remember that apart from the difference in CPU performance, the CPU load is calculated based on the number of cores! There is a saying, "How many cores, how many load".

4. What is the ideal CPU usage?

In the past, the CPU utilization was often considered by us laymen to be a criterion for judging whether the machine has reached full load. I saw that the CPU utilization rate was 60-80% for a long time, and it was considered that the machine had a bottleneck.



https://blog.csdn.net/andylauren/article/details/52561851



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839672&siteId=291194637