当前位置:网站首页>Interview disassembly: how to check the soaring usage of CPU after the system goes online?

Interview disassembly: how to check the soaring usage of CPU after the system goes online?

2022-07-04 13:54:00 Misty jam

Hello everyone , I am ethereal .

Last time the interviewer asked a question : After the application goes online Cpu How to check the soaring usage ?

In fact, this is a very common problem , It's very simple , In that case, why should I write ? Because last time I answered, I forgot to thread PID convert to 16 Hexadecimal command .

So I decided to review this question again , Of course, I also prepared the test code for you , You can actually operate it , So I won't forget next time .

Simulate a high CPU scene

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 Rizhilu ",i);

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

stay main Method opens a thread , Infinite build HighCpu object .

@Data
@AllArgsConstructor
public class HignCpu {
    private String name;
    private int age;
}

Prepare the above code , function HighCpuTest, Then you can start some column operations to find the cause of the problem .

Troubleshooting steps

First step , Use top Find occupancy CPU The highest Java process

1.  monitor cpu Running state , Displays a list of process running information 
top -c

2.  Press CPU Usage sort , Type in uppercase P
P
fa9a0608340ba6c5b1aefa4e049da2a4.png

The second step , use top -Hp Command view occupancy CPU The highest thread

The last step is to use top The order found that Java process . There are so many threads in that process , It is impossible that all threads are occupied all the time CPU Don't put , This step is to find out the culprit , Of course, there may be more than one .

perform top -Hp pid command ,pid It's the one in front Java process , In my case 16738 , The complete order is :

top -Hp 16738, Then type. P ( Capitalization p), According to the thread CPU Usage sort

The effect after execution is as follows

c14d5285c65e55b13c5bca7fb77e1e01.png

It's found that CPU The highest thread PID by 16756

The third step , View stack information , Locate the corresponding code

adopt printf The command converts it into 16 Base number , The reason why it needs to be transformed into 16 Base number , Because in the stack , Threads id Yes, it is 16 In base notation .( I just forgot this order ~)

[[email protected] ~]# printf "%x\n" 16756
4174

obtain 16 Base thread ID by 4174.

adopt jstack Command to view stack information

jstack 16738 | grep '0x4174' -C10 --color
04a6954a896631ef6d260dcb5a18035b.png

Pictured above , Find the consumption CPU The thread name corresponding to the high thread “HighCpu”, And see the stack where the thread is executing the code .

Last , According to the information in the stack , Locate the corresponding dead loop code , Get it done .

Summary

cpu How to check after the usage rate soars This question is not only often asked in interviews , And it is also very useful in practical work , You'd better practice according to the above steps , Only in this way can we remember well .

This article from the Knowledge planet interview disassembly series , Welcome to play together ~

870df75144fdc1cd31c3bc9734a06667.png
原网站

版权声明
本文为[Misty jam]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041051572927.html