The system runs a high CPU alarm, generally can be found through the top or task manager which processes are using the CPU, but this granularity does not let us know more, we need to find out which part of the program is CPU-intensive and what is occupied by the CPU, so as to facilitate our future tuning, Here is a practical idea:
- Find the process that consumes high CPU p
- Find the thread that consumes high CPU in process P t
- Find out what the thread T is doing
This article takes Linux under Java application as an example, other environment or program can refer to this idea, as long as the idea is clear and easy to run
- Open top, press shift+p in top to display the process in reverse of CPU usage, find the CPU-intensive process number PID, and exit top
- Top-h-P PID Display the process of the operation of the thread, the PID can specify more than one, in the top of the shift+p according to the CPU in reverse display threads, to find high CPU thread tid, for example, 27890, print out the TID by printf is the hexadecimal number
printf"%x\n"278906cf2
- Using the Threaddump tool to view the current stack information for the thread in hexadecimal, the Threaddump tool in Java is Jstack
-l<pid>|-i0x6cf2-A15
- Repeat the 1-3 steps several times to determine the time-consuming operation of the thread, noting that the shorter the delay between steps 1-3 is more accurate, preferably under multiple shell windows
By doing this, we can determine which code is consuming CPU resources, and the results are as follows
"Thread-12"Daemon prio=TenTid=0x00007f926c021000Nid=0xba7runnable [0x00007f92c06d7000] Java. Lang. Thread. State: RUNNABLE at Java. IO. FileOutputStream. Writebytes(Native Method) at Java. IO. FileOutputStream. Write(FileOutputStream. Java:282) at Java. IO. Bufferedoutputstream. Flushbuffer(Bufferedoutputstream. Java: $) at Java. IO. Bufferedoutputstream. Flush(Bufferedoutputstream. Java:123)-Locked <0x0000000780d79ae0> (A Java. IO. Bufferedoutputstream) at Java. IO. PrintStream. Write(PrintStream. Java:432)-Locked <0x0000000780d79ac0> (A Java. IO. PrintStream) at Sun. NiO. CS. Streamencoder. Writebytes(Streamencoder. Java:202) at Sun. NiO. CS. Streamencoder. Implflushbuffer(Streamencoder. Java:272) at Sun. NiO. CS. Streamencoder. Flushbuffer(Streamencoder. Java: -)-Locked <0X0000000780D79C10> (A Java. IO. OutputStreamWriter) at Java. IO. OutputStreamWriter. Flushbuffer(OutputStreamWriter. Java:168) at Java. IO. PrintStream. Write(PrintStream. Java:477)-Locked <0x0000000780d79ac0> (A Java. IO. PrintStream) at Java. IO. PrintStream. Print(PrintStream. Java:619) at Java. IO. PrintStream. println(PrintStream. Java:773)-Locked <0x0000000780d79ac0> (A Java. IO. PrintStream) at Org. Apache. Tomcat. Util. Log. Systemloghandler. println(Systemloghandler. Java:242) at Java. Lang. Throwable. Printstacktrace(Throwable. Java:461)-Locked <0x0000000780d79a98> (A org. Apache. Tomcat. Util. Log. Systemloghandler) at Java. Lang. Throwable. Printstacktrace(Throwable. Java:451) atcom. Zhaopin. Framework. Cache. Cachelisthandler$TimeoutTimerThread. Run(Cachelisthandler. Java:101) at Java. Lang. Thread. Run(Thread. Java:662)
The above is only a troubleshooting idea, non-Java or Linux can be done by similar tools, such as Windows can be replaced by the Process Explorer top view processes and thread-related content, etc.
Java application high CPU fault diagnosis (troubleshooting) Ideas