background

In the development process, we are often prone to encounter some faults to troubleshoot, especially in the online system. How to quickly and accurately troubleshoot the system problems is often an effective way for an engineer to solve problems. Therefore, for those of you who have a certain pursuit of technology, it is particularly important to master the corresponding problem positioning and problem-solving ability;

Next, I will briefly describe how to use the command line tool to check the specific thread distribution of applications that occupy a lot of system resources. The following

steps

1. Run the top command to query the processes that occupy the highest CPU usage

top
Copy the code

2. Locate the application based on the process ID

ps -aux|grep PID
Copy the code

3. Locate the thread or code

ps -mp PID -o THREAD,tid,time
Copy the code

In the following screenshot, you can view the CPU consuming threads. The TID is the thread id, and the time is the duration.

As you can see from the figure above, thread 31244 occupies 1.4% of the CPU and lasts 2 minutes and 1 second.

4. Convert the thread number to hexadecimal format. TID indicates the number of the thread to be queried

// Command template: printf "%x\n" TID
printf "%x\n" 31245
Copy the code

5. Displays the stack information of the thread

jstack PID |grep TID -A 60 jstack 31240 |grep 7a0d -A 60
Copy the code

You can see the corresponding stack, as well as the lines of code. Then go to your project to view the source code analysis

(Due to the lack of suitable scene verification, the following example screenshot is from the network)

Finally, summarize the methods and skills to troubleshoot CPU faults:

  1. Top command: Linux command. You can view real-time CPU usage. You can also view the CPU usage in a recent period.
  2. PS command: Linux command. Powerful process status monitoring commands. You can view the current CPU usage of the process and the threads in the process. Sample data belonging to the current state.
  3. Jstack: command provided by Java. You can view the current thread stack status of a process. The output of this command can be used to locate the current running status of all threads of a process, the running code, whether they are deadlocked, and so on.
  4. Pstack: Linux command. You can view the current thread stack status of a process.