java程序cpu占用过高问题分析
- - Web前端 - ITeye博客针对某个java程序cpu占用过高问题分析,要想找到问题的真正原因,首先要明确cpu过高的进程,通过对进程下线程的分析,定位到具体的应用代码,从而定位问题的原因所在. 在jdk自带的分析工具中,通过jconsole只能分析到应用程序的相关系统资源使用情况,但无法定位应用程序,故通过此工具了解到应用程序存在问题,但要具体定位到哪块程序不合理造成的是很困难的.
```` BTRACE_HOME=/root/btrace export BTRACE_HOME export PATH=$PATH:$BTRACE_HOME/bin ````
```` [root@es1 ~]# btrace --version BTrace v.1.3.10 (20171121) ````
```` [root@es1 build]# btrace Usage: btrace <options> <pid> <btrace source or .class file> <btrace arguments> where possible options include: --version Show the version -v Run in verbose mode -o <file> The path to store the probe output (will disable showing the output in console) -u Run in trusted mode -d <path> Dump the instrumented classes to the specified path -pd <path> The search path for the probe XML descriptors -classpath <path> Specify where to find user class files and annotation processors -cp <path> Specify where to find user class files and annotation processors -I <path> Specify where to find include files -p <port> Specify port to which the btrace agent listens for clients -statsd <host[:port]> Specify the statsd server, if any ````
```` [root@es1 btrace]# ll build/ 总用量 1572 -rw-r--r--. 1 root root 460271 12月 16 00:44 btrace-agent.jar -rw-r--r--. 1 root root 358679 12月 16 00:44 btrace-boot.jar -rw-r--r--. 1 root root 785219 12月 16 00:44 btrace-client.jar ````
```` package com.test; import java.util.Random; public class BtraceTest { public static Random random=new Random(); public int add (int a, int b) { int sum=a+b; System.out.println("和:"+sum); return a+b; } public void run(){ try { while (true) { add(random.nextInt(10), random.nextInt(10)); Thread.sleep(random.nextInt(10) * 100); } }catch (Exception e){ e.printStackTrace(); } } public static void main(String[] args) { new BtraceTest().run(); } } ````
```` package samples; import com.sun.btrace.BTraceUtils; import com.sun.btrace.annotations.*; import com.sun.btrace.annotations.Export; import static com.sun.btrace.BTraceUtils.*; @BTrace public class OnlineDebug { @OnExit//当成程序退出时,执行一些命令 public static void onexit(int code) { println("BTrace program exits! with code: " + code); } @Export //可以用来统计调用次数 public static long counter; @OnMethod(clazz="com.test.BtraceTest", method="add", location=@Location(value=Kind.RETURN)) public static void m(@Self Object self, int a,int b,@Return int result,@Duration long time) { BTraceUtils.println("参数: a: "+a+" b: "+b); BTraceUtils.println("花费时间: "+time*1.0/1000+"ms"); jstack();//打印堆栈信息 counter++; } @OnEvent("1")//通过事件触发,打印当前的程序调用次数 public static void setL1() { BTraceUtils.println("executor count: "+counter); } //监控程序是否走到第22行代码 @OnMethod(clazz = "com.test.BtraceTest", location = @Location(value = Kind.LINE, line = 22)) public static void onBind() { println("执行到第22行"); } //每隔指定时间打印一下调用次数 @OnTimer(5000) public static void run(){ BTraceUtils.println("executor count: "+counter); } // //慎用 监控程序里面是否调用了,java.lang下面的一些类或方法 // @OnMethod(clazz="/java\\.lang\\..*/", method="/.*/") // public static void swingMethods( @ProbeClassName String probeClass, @ProbeMethodName String probeMethod) { // print("entered " + probeClass + "." + probeMethod); // } } ````
```` java -cp xxx.jar com.test.BtraceTest ````
```` 和:8 和:11 和:3 和:12 和:14 和:15 和:8 和:2 和:8 和:7 和:18 和:4 ````
```` chmod +x OnlineDebug.java ````执行jps命令,查看模拟程序的进程id
```` btrace 2139 OnlineDebug.java ````
```` 参数: a: 0 b: 8 花费时间: 101.973ms com.test.BtraceTest.add(BtraceTest.java:11) com.test.BtraceTest.run(BtraceTest.java:17) com.test.BtraceTest.main(BtraceTest.java:28) 参数: a: 3 b: 4 花费时间: 116.611ms com.test.BtraceTest.add(BtraceTest.java:11) com.test.BtraceTest.run(BtraceTest.java:17) com.test.BtraceTest.main(BtraceTest.java:28) 参数: a: 0 b: 2 花费时间: 113.168ms com.test.BtraceTest.add(BtraceTest.java:11) com.test.BtraceTest.run(BtraceTest.java:17) com.test.BtraceTest.main(BtraceTest.java:28) 参数: a: 7 b: 7 花费时间: 113.434ms com.test.BtraceTest.add(BtraceTest.java:11) com.test.BtraceTest.run(BtraceTest.java:17) com.test.BtraceTest.main(BtraceTest.java:28) 参数: a: 6 b: 1 花费时间: 181.184ms com.test.BtraceTest.add(BtraceTest.java:11) com.test.BtraceTest.run(BtraceTest.java:17) com.test.BtraceTest.main(BtraceTest.java:28) 参数: a: 7 b: 6 花费时间: 190.881ms com.test.BtraceTest.add(BtraceTest.java:11) com.test.BtraceTest.run(BtraceTest.java:17) com.test.BtraceTest.main(BtraceTest.java:28) executor count: 44 ````
```` 参数: a: 9 b: 9 花费时间: 246.743ms com.test.BtraceTest.add(BtraceTest.java:11) com.test.BtraceTest.run(BtraceTest.java:17) com.test.BtraceTest.main(BtraceTest.java:28) 参数: a: 3 b: 1 花费时间: 251.039ms ^CPlease enter your option: 1. exit 2. send an event 3. send a named event 4. flush console output 1 ````