The Linux CPU usage is too high
In our daily development, there are often some problems that occupy too much CPU resources. These problems often lead to the slow running of our system, or even the collapse of the system. How do we locate this problem?
-
Step 1 Check the CPU usage of system processes
Use top and ps – aux – sort = – pcpu | head – 10 command to check the process and the CPU
- top
- ps -aux –sort=-pcpu|head -10
-
Locating the problem thread
In the previous step, we can obtain the process ID PID that occupies high CPU resources
Top -h p pid Displays the thread information of a process. -h displays the thread information. -p specifies the PID
Thread ID (TID) with high CPU and memory
-
Analyze the cause of the high thread CPU usage
-
Output thread ID in hexadecimal nID =0x+ hexadecimal thread ID.
Printf ‘%x\n’ 11222 # Output thread ID in hexadecimal
-
Locate the cause of the thread problem
Jstack pid | grep -c ‘0 x900 30 – color print a Java thread thread stack information to check the problem
-
If there are too many threads, the possible cause of the problem is a context switch between threads
-
Take a look at the thread context switch activity at the operating system level with the vmstat command
The CS column represents the number of thread context switches, and the IN column represents the number of CPU interrupts. We found that these two numbers were very high, almost confirming our guess that thread context switches consume a lot of CPU.
-
Pidstat monitors process information:
Pidstat [Option] [< interval >] [< Count >]Copy the code
-
-
View disk df -h
-
Check memory free -m
Conclusion: When the CPU is too high, first locate the process that causes it, and then you can use the top -h -p PID command to locate the specific thread. Second, also through jStack to view the state of the thread, to see the number of threads or thread state, if the number of threads is too much, you can suspect that the thread context switch overhead, we can use vmstat and pidstat these two tools to confirm. \