Interview Talk-How to troubleshoot high CPU usage

Interview Talk-How to troubleshoot high CPU usage

Computing is indispensable in the program. If the environment is not too bad, the CPU can basically support the application. But if you find that the CPU remains high, you need to think about whether there is a problem with the program.

When the server CPU remains high, you can start to locate the problem from the following aspects.

Find the pid of the JAVA process

Method 1: jps

That jar is one of my java programs

[root@iZba13i1mo82ot7a3lhq5oZ ~]# jps
17616 Jps
26016 jar
9353 Bootstrap
26028 Bootstrap
16812 Bootstrap

Method 2: ps -ef|grep apply keywords

The pid found in method one is the same, 26016

[root@iZba13i1mo82ot7a3lhq5oZ ~]# ps -ef|grep acupjava
root 17638 17550 0 22:34 pts/0 00:00:00 grep --color=auto acupjava
root 26016 1 0 July 08? 00:05:11 java -jar acupjava-1.0-SNAPSHOT.jar

Find the thread tid with high CPU in the process

Print out the basic thread thread information, find one or several threads with a high CPU percentage, and remember their tid.

PS: The quality of chestnuts is not good, all 0.0%, don't care~

[root@iZba13i1mo82ot7a3lhq5oZ ~]# ps -mp 26016 -o THREAD,tid,time
root 0.0------00:05:11
root 0.0 19-futex_--26016 00:00:00
root 0.0 19-futex_--26017 00:00:02
root 0.0 19-futex_--26018 00:00:00
root 0.0 19-futex_--26019 00:00:00
root 0.0 19-futex_--26020 00:00:11

Print thread stack

Choose a thread and convert tid from decimal to hexadecimal

[root@iZba13i1mo82ot7a3lhq5oZ ~]# printf "%x\n" 26017

For easy viewing, you can print the thread stack to a file, jstack pid >> file name

Use ls to view the file already exists

[root@iZba13i1mo82ot7a3lhq5oZ ~]# jstack 26016 >> stack.txt
[root@iZba13i1mo82ot7a3lhq5oZ ~]# ls
test stack.txt

Find threads with high CPU usage

Open the file, search for the location of tid, you can see the thread stack, and analyze and locate the code that may be problematic.

"http-nio-9527-AsyncTimeout" #29 daemon prio=5 os_prio=0 tid=0x00007fbf68973800 nid=0x65a1 waiting on condition [0x00007fbf48ab0000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at org.apache.coyote.AbstractProtocol$

In this way, the problem of high CPU can basically be located. (PS: The above data has been desensitized slightly)