cpu utilization
First, what is cpu utilization? I have summarized it before Knowledge points related to cpu utilization . Generally, you can check the cpu utilization of the process through the top command. Sometimes you will find that the cpu utilization of a process exceeds 100%. For example:
top - 10:12:55 up 3 days, 13:09, 1 user, load average: 1.19, 0.87, 1.43 Tasks: 235 total, 1 running, 181 sleeping, 0 stopped, 1 zombie %Cpu(s): 56.4 us, 2.6 sy, 0.0 ni, 40.5 id, 0.1 wa, 0.0 hi, 0.3 si, 0.0 st KiB Mem : 7868576 total, 1022508 free, 2635576 used, 4210492 buff/cache KiB Swap: 0 total, 0 free, 0 used. 4484128 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 17069 root 20 0 31840 1564 1412 S 199.0 0.0 0:46.34 samples_test
Why does the% cpu here exceed 100%? Even if the cpu keeps running, it's up to 100%?
At this time, we need to understand the specific meaning of this field:
$ man top ...... 1. %CPU -- CPU Usage The task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time. In a true SMP environment, if a process is multi-threaded and top is not operating in Threads mode, amounts greater than 100% may be reported. You toggle Threads mode with the `H' inter‐ active command. Also for multi-processor environments, if Irix mode is Off, top will operate in Solaris mode where a task's cpu usage will be divided by the total number of CPUs. You toggle Irix/Solaris modes with the `I' interactive command. ......
As can be seen from the above explanation,% CPU represents the percentage of CPU time occupied since the last update. In the actual SMP environment, if the process has multiple threads and the top command is not in thread mode, the result may be greater than 100%.
SMP (symmetric multi processing) here is a symmetric multiprocessor structure. It is characterized by only one operating system instance running on multiple CPUs. The structure of each CPU is the same, and memory and resources are shared.
For thread mode, because the top command displays the data of the process by default, if you use top -H, this option will list all Linux threads. If top is already running, you can also press the "H" key to switch the thread viewing mode to on or off.
Therefore, the% cpu here actually shows the percentage of processes occupying one core, not the percentage of all CPUs. Sometimes it is greater than 100 because the process enables multithreading and occupies multiple cores.
View the cpu usage of each thread in the process
- top -H, or press h after top is running to start thread mode.
You can see the sample_test contains at least two threads with high cpu utilization. The utilization rate is close to 100%, but it will not exceed 100%.
top - 10:13:40 up 3 days, 13:10, 1 user, load average: 1.88, 1.10, 1.49 Threads: 1018 total, 4 running, 961 sleeping, 0 stopped, 1 zombie %Cpu(s): 56.8 us, 1.6 sy, 0.0 ni, 41.3 id, 0.3 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 7868576 total, 1009804 free, 2643240 used, 4215532 buff/cache KiB Swap: 0 total, 0 free, 0 used. 4472252 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 17070 root 20 0 31840 1564 1412 R 99.3 0.0 1:07.91 samples_test 17071 root 20 0 31840 1564 1412 R 98.0 0.0 1:07.83 samples_test
- top -H -p pid number
You can see the actual samples_test has a total of three threads, two of which have particularly high cpu utilization and one is particularly low.
top - 10:40:41 up 3 days, 13:37, 1 user, load average: 2.65, 2.75, 2.44 Threads: 3 total, 2 running, 1 sleeping, 0 stopped, 0 zombie %Cpu(s): 56.2 us, 2.2 sy, 0.0 ni, 41.6 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 7868576 total, 332464 free, 3167324 used, 4368788 buff/cache KiB Swap: 0 total, 0 free, 0 used. 3846852 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2651 root 20 0 31840 1680 1528 R 99.3 0.0 17:27.23 samples_test 2650 root 20 0 31840 1680 1528 R 99.0 0.0 17:26.73 samples_test 2649 root 20 0 31840 1680 1528 S 0.0 0.0 0:00.00 samples_test
You can use the pstree command to quickly view the relationship between threads in a process:
$ pstree -p 2649 samples_test(2649)─┬─{samples_test}(2650) └─{samples_test}(2651)
It can be seen that the thread with thread number 2649 is the main thread, and the cpu utilization is low; The 2650 and 2651 threads are sub threads, and the cpu utilization rate is very high. This is consistent with our code.
samples_test code:
#include <chrono> #include <thread> void thread1() { while(true) {} } void thread2() { while(true) { //std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } int main() { std::thread th1(thread1); std::thread th2(thread2); th1.join(); th2.join(); return 0; }