首页 > 编程语言 >Java火焰图简单学习

Java火焰图简单学习

时间:2022-12-03 22:13:44浏览次数:41  
标签:profiling Java -- sh profiler 学习 misses 火焰 java

前言

立的flags倒了太多次
自己还是太菜了..
课题太大, 自己简单总结一下.
要是自己总结错了.
就不收费, 错就错了 !-_-!

第一步准备环境

一定要设置对java的HOME以及PATH路径. 
如果 which java 不在 自己定义的PATH里面,建议改名.
避免程序出奇奇怪怪的问题. 
which java
可以执行命令
mv /usr/bin/java /usr/bin/java2
创建环境变量.
cat  >  /etc/profile.d/java.sh <<EOF
export PATH=$PATH:/gscloud/server/runtime/java/x86_64-linux/bin
export JAVA_HOME=/gscloud/server/runtime/java/x86_64-linux
EOF

source /etc/profile.d/java.sh
# 注意这里面是我的产品的环境变量.
使用 java -version
确认java的路径是正确的. 版本号也是正常的.

第二步下载相关资源

  • 注意这里我学习的非常弱鸡.
  • 感谢 Brendan Gregg
async-profiler
honest-profiler
lightweight-java-profiler
FlameGraph

第三步各种命令学习

这里掌握的非常浅层. 只能是简单记录,其他的真白瞎.
async-profiler

功能帮助:
Usage: ./profiler.sh [action] [options] <pid>
Actions:
  start             start profiling and return immediately
  resume            resume profiling without resetting collected data
  stop              stop profiling
  dump              dump collected data without stopping profiling session
  check             check if the specified profiling event is available
  status            print profiling status
  meminfo           print profiler memory stats
  list              list profiling events supported by the target JVM
  collect           collect profile for the specified period of time
                    and then stop (default action)
Options:
  -e event          profiling event: cpu|alloc|lock|cache-misses etc.
  -d duration       run profiling for <duration> seconds
  -f filename       dump output to <filename>
  -i interval       sampling interval in nanoseconds
  -j jstackdepth    maximum Java stack depth
  -t                profile different threads separately
  -s                simple class names instead of FQN
  -g                print method signatures
  -a                annotate Java methods
  -l                prepend library names
  -o fmt            output format: flat|traces|collapsed|flamegraph|tree|jfr
  -I include        output only stack traces containing the specified pattern
  -X exclude        exclude stack traces with the specified pattern
  -v, --version     display version string

  --title string    FlameGraph title
  --minwidth pct    skip frames smaller than pct%
  --reverse         generate stack-reversed FlameGraph / Call tree

  --loop time       run profiler in a loop
  --alloc bytes     allocation profiling interval in bytes
  --live            build allocation profile from live objects only
  --lock duration   lock profiling threshold in nanoseconds
  --total           accumulate the total value (time, bytes, etc.)
  --all-user        only include user-mode events
  --sched           group threads by scheduling policy
  --cstack mode     how to traverse C stack: fp|dwarf|lbr|no
  --begin function  begin profiling when function is executed
  --end function    end profiling when function is executed
  --ttsp            time-to-safepoint profiling
  --jfrsync config  synchronize profiler with JFR recording
  --lib path        full path to libasyncProfiler.so in the container
  --fdtransfer      use fdtransfer to serve perf requests
                    from the non-privileged target

注意最后还会有几个样例

Example: ./profiler.sh -d 30 -f profile.html 3456
         ./profiler.sh start -i 999000 jps
         ./profiler.sh stop -o flat jps
         ./profiler.sh -d 5 -e alloc MyAppName

最简单的打印火焰图的方法

可以在PATH里面加上profiler.sh 所在的目录 然后执行命令如下:
profiler.sh -d 60 -f /root/zhaobsh.html `jps |grep caf |head -n 1 |awk '{print $1}'`
简介介绍
-d duration 持续时间, 单位秒钟
-f 执行结果的文件路径. 注意文件后缀很重要, 只有html的才会解析成火焰图. 其他可能不会解析.
最后面放上pid

查看其他的火焰图

profiler.sh -d 10 -e alloc  -f /root/profiler_alloc.html  `jps |grep caf |head -n 1 |awk '{print $1}'`
profiler.sh -d 10 -e lock  -f /root/profiler_lock.html  `jps |grep caf |head -n 1 |awk '{print $1}'`
profiler.sh -d 10 -e cache-misses  -f /root/profiler_cache-misses.html  `jps |grep caf |head -n 1 |awk '{print $1}'`
-e 指定监控的时间. alloc 应该是监控内存分配
- lock监控锁的情况
- cache-miss 监控缓存失败的部分. 

注意profiler可以支持的event主要如下

可以使用 profier.sh list 的方式进行查看
Basic events:
  cpu
  alloc
  lock
  wall
  itimer
Java method calls:
  ClassName.methodName
Perf events:
  page-faults
  context-switches
  cycles
  instructions
  cache-references
  cache-misses
  branch-instructions
  branch-misses
  bus-cycles
  L1-dcache-load-misses
  LLC-load-misses
  dTLB-load-misses
  rNNN
  pmu/event-descriptor/
  mem:breakpoint
  trace:tracepoint
  kprobe:func
  uprobe:path

honest-profiler 的简单使用

解压缩后会有几个文件
console
gui
honest-profiler.jar
liblagent.so

可以通过agent的方式进行拉取
感觉这种方式比较复杂. 

agent的方式运行

java -agentpath:/honest-profiler/liblagent.so=interval=7,
logPath=/tmp/log.hpl -cp Cpu-0.0.1-SNAPSHOT.jar 

注意需要使用绝对路径指向 honest-profiler指定的路径 

结果分析

记得服务不停止 不会产生具体的文件
分析需要使用 flamegraph的角度尽心分析.
主要命令如下: 
第一步需要折叠一下对应的结果 便于flamegraph进行分析
java -cp /honest-profiler/honest-profiler.jar 
com.insightfullogic.honest_profiler.ports.console.FlameGraphDumperApplication
/tmp/log.hpl /tmp/log.folded
第二步进行flamegraph的分析
/FlameGraph/flamegraph.pl /tmp/log.folded > /tmp/flamegraph-java.svg

# 注意这里的命令学习自: 
https://zhuanlan.zhihu.com/p/374861737
这个命令我并没有非常严格的去学习与验证 (连续两天熬夜 困死了..)

第四部分: 火焰图的解读

y 轴表示调用栈,每一层都是一个函数。
  调用栈越深,火焰就越高,顶部就是正在执行的函数,
  下方都是它的父函数。
x 轴表示抽样数,如果一个函数在 
  x 轴占据的宽度越宽,就表示它被抽到的次数多,
  即执行的时间长。注意,x 轴不代表时间,
  而是所有的调用栈合并后,按字母顺序排列的。
  
火焰图就是看顶层的哪个函数占据的宽度最大。
只要有"平顶"(plateaus),就表示该函数可能存在性能问题。
注意:颜色没有特殊含义

解读

image


解读

最顶层的函数g()占用 CPU 时间最多。
d()的宽度最大,但是它直接耗用 CPU 的部分很少。
b()和c()没有直接消耗 CPU。
因此,如果要调查性能问题,首先应该调查g(),其次是i()。

另外,从图中可知a()有两个分支b()和h(),
这表明a()里面可能有一个条件语句,而b()分支消耗的 CPU 大大高于h()。
————————————————
Study From https://blog.csdn.net/Ziputi/article/details/118882851
其实都是从 Brendan Greeg那边学到的. 

标签:profiling,Java,--,sh,profiler,学习,misses,火焰,java
From: https://www.cnblogs.com/jinanxiaolaohu/p/16948870.html

相关文章