火焰图分析性能瓶颈java

1,132 阅读2分钟

1. 火焰图:

火焰图用于分析热点代码占用大量cpu,从而导致服务性能下降的情况。如下图,自底向上为调用关系。上层宽度越宽标示当前函数cpu耗时越久,我们关注最宽的函数调用。

image.png

2. 两款常用火焰图生成工具

2.1 arthas

是阿里开源的分析工具

  1. 下载arthas并执行 curl -O https://arthas.aliyun.com/arthas-boot.jar

  2. 找到pid ps -aux |grep attempt_1613964590462_532873_m_000004_0

  3. ,选择找到的pid,进入后执行 java -jar arthas-boot.jar

$ profiler start
Started [cpu] profiling
$ profiler stop
profiler output file: /tmp/demo/arthas-output/20191125-135546.svg
OK

artha的强大功能不止火焰图,可实时反编译,热修改代码,进行hotfix,快速查找问题代码等等,详细看官网介绍 https://arthas.aliyun.com/doc/

2.2 async-profiler

2.2.1 使用

这款工具开箱即用,本人在生产环境中也经常使用。

1.执行 git clone https://github.com/jvm-profiling-tools/async-profiler

  • 使用make编译即可

2.执行./software/java/bin/jps -m |grep attempt_1613964590462_537723_r_000000_0

  • 我们需要找到此进程的pid(通常为很慢的任务),进行采样分析,对于mapreduce任务,需要我们在管理界面找到任务id,登陆任务执行的机器的yarn账户,这里为任务attempt_1613964590462_537723_r_000000_0,可发现其pid
  1. 执行sh profiler.sh -d 200 -f profile.svg 12222 进行采样生成火焰图
  • -d表示采样时间(s),可将时间设置尽量长,避免其他因素使线程等待,随着线程的结束,采样也会自动结束,无需担心。
  • -f 表示生成的文件,一般为svg文件,可在浏览器直接观察
  • 最后的数字为pid,我们需要找到此进程的pid(第二步),进行采样分析
  1. 如下图: 发现在Integer.ValueOf这里占据了最多的时间,那么我们就要具体看看这段代码极其调用逻辑了,是否是创建了过多的对象等。 image.png