系统运行出现高CPU报警,一般可以通过top或者任务管理器找到哪些进程在使用CPU,但这个粒度不能让我们知道得更多,我们需要找到程序的哪部分在占用CPU并且在占用CPU做什么,这样才有利于我们以后调优,下面介绍一种可行的思路:
本文以Linux下Java应用程序为例,其他环境或程序可参照该思路,只要思路清晰就好办了
$ printf "%x\n" 27890
6cf2
$ jstack -l <pid> | grep -i 0x6cf2 -A 15
通过上面的操作我们就可以确定哪段代码在大量占用CPU资源了,得到的结果如下
"Thread-12" daemon prio=10 tid=0x00007f926c021000 nid=0xba7 runnable [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:65)
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:85)
- 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)
at com.zhaopin.framework.cache.CacheListHandler$TimeoutTimerThread.run(CacheListHandler.java:101)
at java.lang.Thread.run(Thread.java:662)
上述仅是一个故障诊断的思路,非Java或者Linux可以通过类似的工具完成,比如windows下可以通过Process Explorer替代top查看进程和线程相关内容等
Java应用程序高CPU故障诊断(troubleshooting)思路
原文地址:http://blog.csdn.net/shawyeok/article/details/45954521