Arthas profiler(使用async-profiler对应用采样,生成火焰图)

wan123 2天前 阅读数 7549 #软件测试

@[toc]

二、命令列表

2.4 profiler(使用async-profiler对应用采样,生成火焰图)

使用场景:

profiler 命令支持生成应用热点的火焰图。本质上是通过不断的采样,然后把收集到的采样结果生成火焰图。

提示

使用生成火焰图

火焰图的含义:

火焰图是基于 perf 结果产生的SVG 图片,用来展示 CPU 的调用栈。

y 轴表示调用栈,每一层都是一个函数。调用栈越深,火焰就越高,顶部就是正在执行的函数,下方都是它的父函数。

x 轴表示抽样数,如果一个函数在 x 轴占据的宽度越宽,就表示它被抽到的次数多,即执行的时间长。注意,x 轴不代表时间,而是所有的调用栈合并后,按字母顺序排列的。

火焰图就是看顶层的哪个函数占据的宽度最大。只要有"平顶"(plateaus),就表示该函数可能存在性能问题。

颜色没有特殊含义,因为火焰图表示的是 CPU 的繁忙程度,所以一般选择暖色调。

注意:生成的火焰图要从linux服务器下载到本地然后才能打开,而不是浏览器直接输入IP:端口去打开,那样无效。

profiler 命令支持生成应用热点的火焰图。本质上是通过不断的采样,然后把收集到的采样结果生成火焰图。

profiler 命令基本运行结构是: 
profiler action [actionArg]

profiler 命令的格式基本与上游项目 保持一致,详细的使用方式可参考上游项目的 README、Github Disscussions 以及其他文档资料。

参数说明:

参数名称 参数说明
action 要执行的操作
actionArg 属性名模式
[i:] 采样间隔(单位:ns)(默认值:10’000’000,即 10 ms)
[f:] 将输出转储到指定路径
[d:] 运行评测指定秒
[e:] 要跟踪哪个事件(cpu, alloc, lock, cache-misses 等),默认是 cpu

常用命令:

profiler 命令作用
profiler start 启动profiler,默认情况下,生成cpu的火焰图
profiler list 显示所有支持的事件
profiler getSamples 获取已采集的sample的数量
profiler status 查看profiler的状态,运行的时间
profiler stop 停止profiler,生成火焰图的结果,指定输出目录和输出格式:svg或html
  • 启动profiler,默认情况下,生成的是 cpu 的火焰图,即 event 为cpu。可以用--event参数指定其他性能分析模式,见下文。

    $ profiler start
    Started [cpu] profiling
    
  • 显示支持的事件

    profiler list
    
  • 获取已采集的sample的数量

    $ profiler getSamples
    23
    
  • 查看profiler状态(可以查看当前profiler在采样哪种event和采样时间。)

    $ profiler status
    [cpu] profiling is running for 4 seconds
    
  • 停止profiler,并同步生成文件(默认在工作目录下的arthas-output目录。)

    $ profiler stop
    profiler output file: /tmp/demo/arthas-output/20240919-155147.svg
    
  • 通过 --file参数来指定输出结果路径,在--file参数指定的文件名后缀为 htmljfr 时,文件格式可以被推断出来。比如--file /tmp/result.html 将自动生成火焰图。

    # 指定生成的文件名以及路径
    profiler stop --file /tmp/result.svg
    
  • 可以用--format指定生成格式,默认情况下,结果是 Flame Graph格式的 html 文件,也可以用 -o--format 参数指定其他内容格式,包括 flat、traces、collapsed、flamegraph、tree、jfr。

    profiler stop --format html
    
  • 生成的图

profiler 支持的 events

  • 在不同的平台,不同的 OS 下面,支持的 events 各有不同。比如在 macos 下面:
$ profiler list
Basic events:
  cpu
  alloc
  lock
  wall
  itimer
  • 在 linux 下面
$ profiler 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

如果遇到 OS 本身的权限/配置问题,然后缺少部分 event,可以参考 。

可以使用 check action 测试某个 event 是否可用,此 action 的参数格式与 start 一致。

可以用--event参数指定要采样的事件,比如 alloc 表示分析内存分配情况:

$ profiler start --event alloc

本人其他相关文章链接

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.

重要信息


  • 随机文章
  • 热门文章
  • 热评文章
热门