Interviewer: How to troubleshoot the soaring CPU usage after the application goes live?

Hello everyone, I'm Misty.

The interviewer asked a question last time: How to troubleshoot the soaring CPU usage after the application goes online?

In fact, this is a very common problem, and it is very simple, so why should I write it? Because when I answered last time I forgot to convert the thread PID to hex command.

So I decided to revisit this question again. Of course, I also prepared the test code for you, so you can actually operate it, so that you will not forget it next time.

Simulate a high CPU scenario

public class HighCpuTest {
    public static void main(String[] args) {
        List<HignCpu> cpus = new ArrayList<>();

        Thread highCpuThread = new Thread(()->{
            int i = 0;
            while (true){
                HignCpu cpu = new HignCpu("Java日知录",i);

                cpus.add(cpu);
                System.out.println("high cpu size:" + cpus.size());
                i ++;
            }
        });
        highCpuThread.setName("HignCpu");
        highCpuThread.start();
    }
}
复制代码

A thread is started in the main method, and the HighCpuobject is constructed infinitely.

@Data
@AllArgsConstructor
public class HignCpu {
    private String name;
    private int age;
}
复制代码

Prepare the above code, run HighCpuTest, and then you can start a series of operations to find the cause of the problem.

Troubleshooting steps

The first step is to use top to find the Java process with the highest CPU usage

1. 监控cpu运行状,显示进程运行信息列表
top -c

2. 按CPU使用率排序,键入大写的P
P
复制代码

image-20220627165915946

The second step, use the top -Hpcommand to view the thread that occupies the highest CPU

The previous step found that Java process with the topcommand . There are so many threads in that process, and it is impossible for all threads to occupy the CPU all the time. What we need to do in this step is to find out the culprit. Of course, there may be more than one.

Execute the top -Hp pidcommand, pid is the previous Java process, in my example 16738, the complete command is:

top -Hp 16738, then type P (uppercase p), threads are sorted by CPU usage

The effect after execution is as follows

image-20220627165953456

It is found that the PID of the thread that occupies the highest CPU is 16756

The third step is to view the stack information and locate the corresponding code

Convert it to hexadecimal through the printf command. The reason why it needs to be converted to hexadecimal is because the thread id in the stack is represented in hexadecimal. (I just forgot about this command at the time~)

[root@review-dev ~]# printf "%x\n" 16756
4174
复制代码

The thread ID obtained in hexadecimal is 4174.

View stack information through the jstack command

jstack 16738 | grep '0x4174' -C10 --color
复制代码

image-20220627170218909

As shown in the figure above, the thread name "HighCpu" corresponding to the thread with high CPU consumption is found, and the stack of the code being executed by the thread is seen.

Finally, according to the information in the stack, locate the corresponding infinite loop code and get it done.

summary

cpu使用率飙升后如何排查这个问题不仅面试中经常会问,而且在实际工作中也非常有用,大家最好根据上述步骤实际操作一下,这样才能记得住记得牢。

我正在参与掘金技术社区创作者签约计划招募活动,点击链接报名投稿

Guess you like

Origin juejin.im/post/7119116752939646984